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