开发中向服务器请求到的数据是特别复杂的,需要从中抽离出需要展示的数据进行展示个和交互。

思路:

先将请求到的复杂数据传递到一个类A中,从类A里抽离出需要的数据。需要展示数据的地方,面向类A开发,类A关心数据抽离问题,不关心如何展示,只管提供所需要的数据。

页面关心展示数据问题,不关心数据问题,只管和类A要数据。

实现:

目录结构:

src  > network > request.js

         > detail.js

  > detail.vue

detail.vue向detail.js要需要展示的数据,detail.js请求数据,并从复杂数据中抽离出detail.vue所需要的数据。

代码:

<template>
<div id= "detail">
// 展示数据 </div>
</template> <script> import {getDetail, Goods,Shop,GoodsParam,getRecommend} from 'network/detail' export default {
name: "Detail", data() {
return {
iid: null,
topImages: [],
goods: {},
shop:{},
detailInfo:{},
paramInfo:{},
commentInfo:{},
recommends: [],
itemImgListener:null,
}
}, created() {
// 1. 保存传入的iid
console.log("bb",this.$route.params)
this.iid = this.$route.params.iid // 2.根据iid请求详情数据
getDetail(this.iid).then(res => {
console.log(res)
// 1. 轮播图数据
const data = res.result;
this.topImages = data.itemInfo.topImages
// 2.获取商品信息
this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services) // 3. 创建店铺信息的对象
this.shop = new Shop(data.shopInfo) // 4. 保存商品的详情数据
this.detailInfo = data.detailInfo; //5. 获取参数信息
this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule) // 6. 获取评论信息
if (data.rate.cRate !== 0){
this.commentInfo = data.rate.list[0]
} // 7.请求推荐数据
getRecommend().then(res => {
console.log('详细页面的商品推荐数据',res)
this.recommends = res.data.list
})
},
</script>

detail.vue

import axios from "axios"

// 推荐写法,因为axios返回的就是promise对象,没必要在封装一次promise
// 如果换axios框架,只需本页去掉axios相关,导入最新框架,return new Promise()就可以,其他文件依旧正常使用
export function request(config) {
const instance = axios.create({
// baseURL: 'http://123.207.32.32:8000/api/hy',
baseURL: 'http://106.54.54.237:8000/api/hy',
timeout: 5000
}); // 2.2 响应拦截
instance.interceptors.response.use(res =>{
// console.log('响应拦截');
// console.log(res.data);
return res.data
},err =>{
console.log('拦截服务器响应错误')
console.log(err)
}) // 发送网络请求
return instance(config)
}

request.js

import {request} from './request'

export function getDetail(iid){
return request({
url: "/detail",
params: {
iid
}
})
} export class Goods{
constructor(itemInfo, columns, services){
this.title = itemInfo.title;
this.desc = itemInfo.desc;
this.newPrice = itemInfo.price;
this.oldPrice = itemInfo.oldPrice;
this.discount = itemInfo.discountDesc;
this.columns = columns;
this.services = services;
this.realPrice = itemInfo.lowNowPrice;
}
} export class Shop{
constructor(shopInfo){
this.logo = shopInfo.shopLogo;
this.name = shopInfo.name;
this.fans = shopInfo.cFans;
this.sells = shopInfo.cSell;
this.score = shopInfo.score;
this.goodsCount = shopInfo.cGoods;
}
} export class GoodsParam {
constructor(info, rule) {
// 注: images可能没有值(某些商品有值, 某些没有值)
this.image = info.images ? info.images[0] : '';
this.infos = info.set;
this.sizes = rule.tables;
}
} export function getRecommend(){
return request({
url: '/recommend'
})
}

detail.js

web开发网络请求到数据的整合办法的更多相关文章

  1. Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据

    版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...

  2. iOS开发网络篇—JSON数据的解析

    iOS开发网络篇—JSON数据的解析 iOS开发网络篇—JSON介绍 一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式 ...

  3. Flutter网络请求和数据解析

    一:前言 - 什么是反射机制,Flutter为什么禁用反射机制? 在Flutter中它的网络请求和数据解析稍微的比较麻烦一点,因为Flutter不支持反射机制.相信大家都看到这么一条,就是Flutte ...

  4. Android之三种网络请求解析数据(最佳案例)

    AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...

  5. ASP.NET Web API 记录请求响应数据到日志的一个方法

    原文:http://blog.bossma.cn/dotnet/asp-net-web-api-log-request-response/ ASP.NET Web API 记录请求响应数据到日志的一个 ...

  6. (转载)Android之三种网络请求解析数据(最佳案例)

    [置顶] Android之三种网络请求解析数据(最佳案例) 2016-07-25 18:02 4725人阅读 评论(0) 收藏 举报  分类: Gson.Gson解析(1)  版权声明:本文为博主原创 ...

  7. iOS开发网络请求——大文件的多线程断点下载

    iOS开发中网络请求技术已经是移动app必备技术,而网络中文件传输就是其中重点了.网络文件传输对移动客户端而言主要分为文件的上传和下载.作为开发者从技术角度会将文件分为小文件和大文件.小文件因为文件大 ...

  8. iOS开发网络篇—XML数据的解析

     iOS开发网络篇—XML数据的解析 iOS开发网络篇—XML介绍 一.XML简单介绍 XML:全称是Extensible Markup Language,译作“可扩展标记语言” 跟JSON一样,也是 ...

  9. Solon Web 开发,五、数据访问、事务与缓存应用

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

随机推荐

  1. Dotnet core使用JWT认证授权最佳实践(二)

    最近,团队的小伙伴们在做项目时,需要用到JWT认证.遂根据自己的经验,整理成了这篇文章,用来帮助理清JWT认证的原理和代码编写操作. 第一部分:Dotnet core使用JWT认证授权最佳实践(一) ...

  2. 让.NetCore程序跑在任何有docker的地方

    一.分别在Windows/Mac/Centos上安装Docker Windows上下载地址:https://docs.docker.com/docker-for-windows/install/(wi ...

  3. day06:三级菜单练习0218

    #1:省份数列:data = { "北京":{ "昌平":{ "沙河":["oldboy","电信" ...

  4. E. Physical Education Lessons 动态开辟线段树区间更新

    E. Physical Education Lessons time limit per test 1 second memory limit per test 256 megabytes input ...

  5. tp5插入百万条数据处理优化

    <?php namespace app\index\controller; use think\Controller; use think\Db; class Charu extends Con ...

  6. Spring基础之IOC

    一.ioc能解决什么问题 1.Spring是什么 spring是以ioc和aop为核心,能整合第三方框架和类库的企业级应用开源框架. 2.程序的耦合问题 例子:Driver类必须存在,编译才通过,Jd ...

  7. 【数据结构的JavaScript版实现】data-struct-js的npm包初版作成

    [数据结构的JavaScript版实现]data-struct-js的npm包初版作成 码路工人 CoderMonkey [数据结构的JavaScript版实现] 拖了这么久,终于趁着春节假期把初版( ...

  8. 又抓了一个导致频繁GC的鬼--数组动态扩容

    概述 本周有个同事过来咨询一个比较诡异的gc问题,大概现象是,系统一直在做cms gc,但是老生代一直不降下去,但是执行一次jmap -histo:live之后,也就是主动触发一次full gc之后, ...

  9. 读了这一篇,让你少踩 ArrayList 的那些坑

    我是风筝,公众号「古时的风筝」,一个不只有技术的技术公众号,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者. Spring Cloud 系列文章已经完 ...

  10. Java IO(十九)PrintStream 和 PrintWriter

    Java IO(十九)PrintStream 和 PrintWriter 一.介绍 (一).PrintStream PrintStream 是打印输出流,它继承于FilterOutputStream. ...