没有合适的资源?快使用搜索试试~ 我知道了~
首页浅谈vue使用axios的回调函数中this不指向vue实例,为undefined
今天在vue-cli脚手架搭建的项目中使用axios时,遇到无法解析this.$route的报错信息,最后发现是作用域的问题。 1.解决方法:使用 => 原代码: axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); 修改为: axios.get('/user', { params: { ID: 12345
资源详情
资源评论
资源推荐

浅谈浅谈vue使用使用axios的回调函数中的回调函数中this不指向不指向vue实例实例,为为
undefined
今天在vue-cli脚手架搭建的项目中使用axios时,遇到无法解析this.$route的报错信息,最后发现是作用域的问题。
1.解决方法:使用解决方法:使用 =>
原代码:
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
修改为:
axios.get('/user', {
params: {
ID: 12345
}
})
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
});
2.=>解析解析
在JS中,箭头函数并不是简单的function(){}匿名函数的简写语法糖,实际上,箭头函数和匿名函数有个明显的区别:箭头函
数内部的this是词法作用域,在编写函数时就已经确定了,由上下文确定。而匿名函数的this指向运行时实际调用该方法的对
象,无法在编写函数时确定。
不可以当做构造函数,也就是说,不可以使用 new 命令,否则会抛出错误。
this、arguments、caller等对象在函数体内都不存在。
不可以使用 yield 命令,因此箭头函数不能用作 Generator 函数。
箭头函数除了传入的参数之外,其它的对象都没有!在箭头函数引用了this、arguments或者参数之外的变量,那它们一定不
是箭头函数本身包含的,而是从父级作用域继承的。
补充知识:补充知识:axios 中请求的回调函数中的中请求的回调函数中的this指针问题指针问题
请看下面两组代码
①
this.axios.post(url, data)
.then(function(result) {
var resData = result.data
console.log(resData)
if (resData.status === 1) {
} else {
}
})
.catch(function (error) {
console.log(error)
})
②
this.axios.post(url, data)

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0