小程序经常要绑定函数,在这个绑定的函数内可能还有回调函数。当进入回调函数时this指向会发生变化这是就需要在进入绑定函数时就保存this:let that = this.
例如:
fetchDetail: function(id) {
var that = this;
wx.request({
url: Api.getTopicInfo({
id: id
}),
success: function(res) {
res.data[0].created = Util.formatTime(Util.transLocalTime(res.data[0].created));
that.setData({
detail: res.data[0]
})
}
})
that.fetchReplies(id);
},