在写一个vue项目,想把项目中使用的vue-resource换成vue-axios,语法从http.$http.post变为axios.post,之后,紧接在下面的语句this.$message出现如下报错
 
vue过程中遇到的Uncaught (in promise) TypeError: Cannot read property '$message' of undefined
 
vue过程中遇到的Uncaught (in promise) TypeError: Cannot read property '$message' of undefined

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

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

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

 

 
vue过程中遇到的Uncaught (in promise) TypeError: Cannot read property '$message' of undefined
转载于:https://www.jianshu.com/p/601762eeadad

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-05
  • 2022-12-23
  • 2021-10-20
  • 2021-10-10
猜你喜欢
  • 2021-07-28
  • 2022-12-23
  • 2021-10-10
  • 2021-07-18
  • 2021-11-18
  • 2022-12-23
相关资源
相似解决方案