Home > WeChat Applet > Mini Program Development > Detailed explanation and simple examples of WeChat applet this and that

Detailed explanation and simple examples of WeChat applet this and that

高洛峰
Release: 2017-02-21 17:52:57
Original
2718 people have browsed it
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


##More detailed explanations and simple examples of WeChat mini programs this and that For related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template