Arrow function may change this scope, callback function with the arrow pointing this page, custom events this function with the arrow pointing undefined

1, the callback function, the change in the scope of this function by arrows

Success: (RES) => { 
    this.setData ({// At this time, this point to page the page 
        ... 
    }) 
}    

 

2, custom events, if you use the arrow functions, the arrow points to the function of this undefined, does not point to page

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

We need to be modified to form a traditional function

onLike: function (Event) { 
    likeModel.like (this.data.classic.id) // In this case, point to the this page page, no error 
  },

  

 

Guess you like

Origin www.cnblogs.com/qq254980080/p/11068059.html