今天在使用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);
});
本文介绍了一位开发者在使用axios发送请求并处理响应数据时遇到的'Cannot set property'错误。通过调整回调函数为箭头函数,成功解决了this指向问题,确保了数据正确赋值。
4672

被折叠的 条评论
为什么被折叠?



