Mini Program startup page life cycle

1,onLoad

2,onShow

3,onReady

In the process of WeChat applet development, we need to refresh the current interface most of the time after operating data on a page to display the results of the operation, but how to refresh the page after performing the operation becomes A problematic but much-needed operation. The following introduces several methods for refreshing the current interface of the WeChat applet.

Method 1: this.onLoad()


How to use: Call this.onLoad() or that.onLoad() in the operation function (sometimes you need to define that = this when the scope of this is not enough).

Applicable scenario: This operation has no effect on the various parameters carried by the page onLoad function. At this time, when we execute the onLoad function for the first time on the page, we can define a variable _options to store the parameter options of the onLoad function in this variable. When the operation function is executed, call this.onLoad(_options), if this operation If the options variable of the onLoad function changes the next time the interface is loaded, this method is invalid.

Method 2: this.onReady() or this.onShow() (effective for personal testing)

How to use: call this.onReady() function in the operation function.

Prerequisite: Data operations on this page need to be completed in the page's onReady() or onShow() function.

Method Three: Define Flags

How to use: In app.js, define a global variable Flag, the default value is false, set it to true when entering other pages, add a judgment in the onShow method of the page, if Flag is true, first set its Set the value to false, and then call the interface to get the data. code show as below:

onShow: function(){
      if (app.globalData.Flag) {
            app.globalData.Flag = false;
            this.getData();//调用接口获取数据
      }  
}    

 

Although this method feels a bit complicated and cumbersome, it can indeed solve practical problems.

Guess you like

Origin blog.csdn.net/qq_26467207/article/details/108200552