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
 
 
 
阅读 4.2k
1 个回答

// 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
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题