WeChat applets this and that are detailed and simple examples, and that is detailed

Source: Internet
Author: User

The applet this and that are detailed and simple examples, and that is detailed

In the applet. request ({}); after the method call is successful or fails, sometimes you need to obtain the page initialization data. If you use this. if the data is not obtained, undefiend is reported on the debugging page. The reason is that in javascript, this represents the current object and will change with the context during the execution of the program, in wx. request ({}); In the callback function of the method, the object has changed, so it is no longer wx. request ({}); Method object, and data attribute does not exist. The official solution is to copy the current object as follows:

Var that = this; // copy this object to the temporary variable that

You can use that. data in the success callback function to obtain the data.

However, there is another way, especially, to change the success callback function to a declaration method, as shown below:

success: res =>{  this.setData({      loadingHidden: true,      hideCommitSuccessToast: false  })}

In this way, this can be used directly to obtain data.

Here is a complete example:

Success: res => {if (res. data. code! = 0) {// submission failed this. setData ({loadingHidden: true, hiddenTips: false, tipsContent: res. data. message})} else {// submission successful this. setData ({loadingHidden: true, hideCommitSuccessToast: false}) subBtn = false; // timed, 3 seconds to disappear setTimeout () => {this. setData ({hideCommitSuccessToast: true}) wx. navigateBack ({delta: 2}) ;}, 2000 );}}

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.