箭頭函數中可改變this做用域,回調函數用箭頭函數this指向page,自定義事件用箭頭函數this指向undefined

一、回調函數中,用箭頭函數改變this的做用域函數

success: (res)=>{
    this.setData({     //此時,this指向page頁面
        ...
    })
}    

 

二、自定義事件中,若是使用箭頭函數,則箭頭函數中的this指向undefined,不指向pagethis

onLike: (event)=>{
    likeModel.like(this.data.classic.id)  //報錯,this指向undefined
  },

須要修改爲傳統函數形式blog

onLike: function(event){
    likeModel.like(this.data.classic.id)  //此時,this指向page頁面,不報錯
  },
相關文章
相關標籤/搜索