In the WeChat applet, after the wx.request({}); method is called successfully or fails, sometimes it is necessary to obtain the page initialization data data. At this time, if you use , to obtain this.data, it may not be obtained, and the debugging page will also report undefiend. The reason is that in JavaScript, this represents the current object, which will change with the context during the execution of the program. In the callback function of the wx.request({}); method, the object has changed, so it is no longer wx. request({}); method object, and the data attribute no longer exists. The official solution is to copy the current object, as follows: var that=this;//Copy this object to the temporary variable that in success Data can be obtained by using that.data in the callback function. However, there is another way, which is also very special, which is to change the declaration method of the success callback function, as follows: success: res =>{ this.setData({ loadingHidden: true, hideCommitSuccessToast: false }) } Copy after login In this way, this can be used directly, and the data can be obtained completely . Give me another complete example: success: res => { if (res.data.code != 0) { // 提交失败 this.setData({ loadingHidden: true, hiddenTips: false, tipsContent: res.data.message }) } else { // 提交成功 this.setData({ loadingHidden: true, hideCommitSuccessToast: false }) subBtn = false; // 定时,3秒消失 setTimeout(() => { this.setData({ hideCommitSuccessToast: true }) wx.navigateBack({ delta: 2 }); }, 2000); } } Copy after login |