Cannot set property 'branchdata' of undefined

今天在使用axios时,向服务器发送请求然后将返回的数据赋值给branchdata时控制台报了这样的错

Cannot set property 'branchdata' of undefined

代码如下:

this.$axios
    .get('/demo/org-info/get-branchorg')
    .then(function(res) {
    this.branchdata = res.data.body;
    console.log('--------------------------------------------');
    console.log(res);
    //   console.log(this.branch[0].orgname);
})
    .catch(function(error) {
    console.log(error);
});

后来尝试了很多办法,最终发现使用箭头函数可以解决这个问题

代码如下:

this.$axios
    .get('/demo/org-info/get-branchorg')
    .then(res => {
    this.branchdata = res.data.body;
})
    .catch(function(error) {
    console.log(error);
});

猜你喜欢

转载自www.cnblogs.com/wugongzi/p/13386621.html