The life cycle of wechat applet is divided into: Application lifecycle (APP) and Page life cycle (page) 。
1、 Applet lifecycle
The lifecycle function of the applet application is in the app.js File.
The app () function is used to register the applet and receives an object as a parameter that specifies the applet's lifecycle callback.
Calling order of applet life cycle: Onlaunch (when the applet initialization is completed and the global trigger is only once) > Onshow (when the applet starts or enters the foreground display from the background) > Onhide (when the applet enters the background from the foreground)
When entering the applet for the first time, the wechat client will initialize the running environment of the applet first. After the initialization is completed, the wechat client will dispatch the app instance of the logic layer onLaunch Event, the method is called.
When the applet load is displayed, it will be executed onShow method. If you go back to the applet from the background, it will Execute the method again 。
If you click the capsule close button in the upper right corner, or directly press the home key of the mobile phone to leave the applet, the small program will enter the background state, which will trigger onHide method.
2、 Page lifecycle
The page life cycle function in each page directory Page name.js Call file.
The page() function is used to register a page. Receives an object as a parameter, which specifies the initial data of the page, lifecycle callbacks, event handling functions, and so on.
The lifecycle function of is called every time you enter or switch to a new page.
Call order of page life cycle: Onload (when the page is first loaded, it will only be called once before the page is destroyed) > Onshow (after the page is displayed) > On ready (the first rendering of the page is completed. Similarly, it is called only once before the page is destroyed) > Onhide (the page is hidden or cut into the background) / onunload (page unloading)
The first time the page is loaded, the logical layer is dispatched by the configuration instance onLoad The method is called.
When the page is displayed, it is executed onShow method. If you return to the applet from the background, or leave this page, and then return to this page from another page, the Execute the method again 。
When the first rendering of the page is complete, onReady Method will be triggered. After this method is triggered, the representative page is ready, and the logical layer can interact with the view layer.
If you jump to a new page on the basis of retaining the current page, or switch from the bottom tab bar to another page, or the applet cuts into the background, it will trigger onHide method.
If you close the current page or close and return to the previous page, the current page is triggered onUnload Methods.