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

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

  • 箭头函式并没有自己的 this
  • this 会往上找到最近的函数主体作为物件
  • 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/


<<:  为了转生而点技能-JavaScript,day16(this指向练习

>>:  Dot NET Core Host - 运作概述图解

[ 卡卡 DAY 5 ] - Style in React Native - inline style vs StyleSheet

在 React Native 提供两种方式引入样式 写 inline style 运用官方提供的 ...

D6 第三周 (回忆篇)

这礼拜进度比较快写到 week4 的作业。看了一下 fetch API 的东西,$.ajax(), ...

[Python 爬虫这样学,一定是大拇指拉!] DAY20 - Python:Requests 基本应用 (3)

这篇是基础应用的最後一篇,主要补充讲解几个基本功能。 检查 Response Status 当我们在...

[DAY30] 浅谈 Azure Machine Learning 的 MLOps 做法

DAY30 浅谈 Azure Machine Learning 的 MLOps 做法 终於来到最後一...

Day28 ATT&CK for ICS - Command and Control

Command and Control 攻击者已经进入工控环境之後,从自己的服务器传送指令给受害主机...