源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

使用vue-resource进行数据交互的实例

  • 时间:2021-06-25 06:47 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:使用vue-resource进行数据交互的实例
[b]一.使用vue-resource插件进行数据交互式,返回的并不是直接的json数据,其实还封装了一层。[/b] [b]如下代码:直接使用 res.result.list 取不到数据。[/b]
methods:{
  cartview:function(){
   var _this = this;
   this.$http.get("data/cartData.json").then(function(res){
    _this.productList = res.result.list;
    _this.totalMoney = res.result.totalMaoney;
   });
  }
 }
[b]错误信息如下:[/b] [img]http://files.jb51.net/file_images/article/201709/2017090209321513.png[/img] [b]这时进行断点调试:[/b] [b]F12 打开chrome浏览器控制台——ctrl+p ——查找相应的代码文件(car.js 即上面那段代码所在的文件。)——在19行打断点——刷新——鼠标移到res,可以看到整个封装好的结构,这里我们看到result实际上是在data里面。[/b] [img]http://files.jb51.net/file_images/article/201709/2017090209321514.png[/img] [img]http://files.jb51.net/file_images/article/201709/2017090209321515.png[/img] [img]http://files.jb51.net/file_images/article/201709/2017090209321516.png[/img] [b]所以正确获取数据的代码如下:[/b]
methods:{
  cartview:function(){
   var _this = this;
   this.$http.get("data/cartData.json").then(function(res){
    _this.productList = res.data.result.list;
    _this.totalMoney = res.data.result.totalMaoney;
   });
  }
 }
[b]json文件结构如如下[/b]
{
 "status":1,
 "result":{
 "totalMoney":109,
 "list":[
  {
  "productId":"600100002115",
  "productName":"黄鹤楼香烟",
  "productPrice":19,
  "productQuantity":1,
  "productImage":"img/goods-1.jpg",
  "parts":[
   {
   "partsId":"10001",
   "partsName":"打火机",
   "imgSrc":"img/part-1.jpg"
   },
   {
   "partsId":"10002",
   "partsName":"打火机",
   "imgSrc":"img/part-1.jpg"
   }
  ]
  },
  {
  "productId":"600100002120",
  "productName":"加多宝",
  "productPrice":8,
  "productQuantity":5,
  "productImage":"img/goods-2.jpg",
  "parts":[
   {
   "partsId":"20001",
   "partsName":"吸管",
   "imgSrc":"img/part-2.jpg"
   }
  ]
  },
  {
  "productId":"600100002117",
  "productName":"金装黄鹤楼",
  "productPrice":25,
  "productQuantity":2,
  "productImage":"img/goods-1.jpg",
  "parts":[
   {
   "partsId":"10001",
   "partsName":"打火机-1",
   "imgSrc":"img/part-1.jpg"
   },
   {
   "partsId":"10002",
   "partsName":"打火机-2",
   "imgSrc":"img/part-1.jpg"
   }
  ]
  }
 ]
 },
 "message":""
以上这篇使用vue-resource进行数据交互的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部