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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM