Introduction:
I have just come into contact with the mini program recently. During the test, I found that the data in the mini program was not released after exiting the mini program, so when I opened it again When the data is small, the data is not initialized. So how do we solve this problem?
Solution:
First declare a variable isClose in the mini program data data. The default value is true, which is used to determine whether it is opened from the mini program entrance. When the user clicks to jump to the page or closes the applet, the OnHide function will be triggered. At this time, isClose isTrue will be determined in this function, indicating that it will be opened after closing. When the page jumps, isClose will first be set to false, so that when the OnHide function is triggered, isClose is false will not be executed, and then the page will be entered, and then the page will return.
In the OnUnload function, set a timer and change isClose to true after 200ms. In this way, when the applet is closed and re-entered, isClose is still true, and it is judged that the page is entered for the first time.
The life cycle functions of the mini program are as follows:
1.1 Monitoring page loading
onLoad: function (options) {},
A page will only be called once. You can get the query called to open the current page in onLoad. parameter.
1.2 The initial rendering of the monitoring page is completed
onReady: function () {},
A page will only be called once, which means that the page is ready and can interact with the view layer.
1.3 Monitoring page display
onShow: function () {},
Will be called every time the page is opened.
1.3 Listening to page hiding
onHide: function () {},
Called when navigateTo or the bottom tab is switched.
(Learning video sharing: php video tutorial)
1.4 Monitoring page unloading
onUnload: function () {},
Called when redirectTo or navigateBack.
Unique function in app.js of WeChat mini program
2.1 Monitoring mini program initialization
onLaunch:function () {},
When the mini program initialization is completed, onLaunch will be triggered (global trigger only Once)
2.2 Error monitoring function
onError:function () {},
When a script error occurs in the applet, or the api call fails, onError will be triggered with the error message
Related recommendations:小program development tutorial
The above is the detailed content of What are the mini program life cycle functions?. For more information, please follow other related articles on the PHP Chinese website!