javascript - VUE axios all()使用返回值报错

查看:738
本文介绍了javascript - VUE axios all()使用返回值报错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

data(){

  return {
    tabledata: [],
    oftenGoods: [],
  }
},
created: function () {
  axios.all([
    axios.get('test1.php'),
    axios.get('test2.php')
  ]).then(axios.spread(function(oftenResp, typeResp) {
        console.log(oftenResp.data);
        console.log(typeResp.data);
       
        this.tabledata= typeResp.data
    }))
    .catch(error=>{
      console.log(error);
    })
},


返回的两个响应数据都能拿到, 打印也是成功的
 this.tabledata= typeResp.data  //这句赋值的时候报错TypeError: Cannot read property 'tabledata' of null
 
 
 

解决方案

// this.tabledata= typeResp.data 你把这一句写在哪里

我大概明白了,

then(axios.spread(function(oftenResp, typeResp) {
        console.log(oftenResp.data);
        console.log(typeResp.data);
       
        this.tabledata= typeResp.data
    }))
    
    改成
    
    then(axios.spread((oftenResp, typeResp) => {
    console.log(oftenResp.data);
    console.log(typeResp.data);
   
    this.tabledata= typeResp.data
}))

如果支持 ES6语法,的话,哪就
var _that = this
.....

...
_that.tabledata= typeResp.data

这篇关于javascript - VUE axios all()使用返回值报错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆