vue过程中遇到的Uncaught (in promise) TypeError: Cannot read property '$message' of undefined

vue过程中遇到的Uncaught (in promise) TypeError: Cannot read property '$message' of undefined

在写一个vue项目,想把项目中使用的vue-resource换成vue-axios,语法从http.$http.post变为axios.post,之后,紧接在下面的语句this.$message出现如下报错
 
 

原来是http.$http.post时this.$message不报错,改成axios.post时this.$message报错,猜想可能是this指向有变化,后查找发现function(response){this.message...}和(res)=>{this.message...}中this的指向是不同的,箭头函数并不简单是匿名函数的简写:

 

把匿名函数改成箭头函数后成功:

 

 
转载于:https://www.jianshu.com/p/601762eeadad
posted @ 2021-02-24 13:34  xing柠檬  阅读(30627)  评论(0编辑  收藏  举报