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