Cannot set property 'branchdata' of undefined

时间:2022-07-22
本文章向大家介绍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);
});