【JS】Cannot read property 'setData' of undefined & 关于this问题
-
常出现不能使用
this.函数
的问题,考虑是不是 this指代对象 发生错误的问题。 -
解决1:常使用
var that=this;
保存当前this对象,在后面进行使用getUserProfile(e) { var that = this; wx.getUserProfile({ success(res) { that.setData({ userInfo: res.userInfo }) } }) }
-
解决2:使用ES6的箭头函数
getUserProfile(e) { wx.getUserProfile({ success:(res)=>{ this.setData({ userInfo: res.userInfo }) } }) }