【Vue】this = undefined 可能是箭头函式搞的鬼

箭头函式和普通函式之间的区别

箭头函式并没有自己的 thisthis 会往上找到最近的函数主体作为物件this 指向定义时所在的物件,而不是呼叫时所在的物件

实际 mounted 资料

mounted() {  axios.get('https://')  .then(function(res){     this.spots = res.data;     console.log(this);  // undefined  })}
mounted() {  axios.get('https://')  .then((res) => {      this.spots = res.data;      console.log(this);  // Vue   })}

参考来源:
https://iter01.com/141625.html
https://blog.scottchayaa.com/post/2019/03/09/jquery-closure-function-this/


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章