iT邦幫忙

0

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

7514 2021-12-07 23:59:101148 瀏覽
  • 分享至 

  • xImage
  •  

箭頭函式和普通函式之間的區別

  • 箭頭函式並沒有自己的 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/


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言