Home >WeChat Applet >WeChat Development >Teach you to use the app() function to register a program (WeChat program development)
This article mainly introduces the relevant information of the registration process of the WeChat Mini Program Tutorial. Friends who need it can refer to the
series of articles:
Modularization of the WeChat Mini Program Tutorial
Registration page of WeChat mini program tutorial
Registration process of WeChat mini program tutorial
App()
App() function Let’s register for a mini program. Accepts an object parameter, which specifies the life cycle function of the applet, etc.
object parameter description:
Attribute | Type | Description | Trigger timing |
---|---|---|---|
Function | Life cycle function--Listening applet initialization | When the mini program initialization is completed, onLaunch will be triggered (globally only triggered once) | |
Function | Life cycle function- - Listening to the mini program display | When the mini program starts, or enters the foreground display from the background, onShow | |
Function | # will be triggered ##Life cycle function--Monitoring applet hiding | When the applet enters the background from the foreground, onHide will be triggered | |
Any | Developers can add any function or data to the Object parameter, and use this to access |
When the user clicks on the upper left corner to close, or presses the home button of the device to leave WeChat, the mini program is not being destroyed, but enters the background; when the user starts WeChat again or opens the mini program again, it will enter the foreground from the background. .
Only when the mini program enters the background for a certain period of time, or the system resource usage is too high, will it be truly destroyed.
Sample code:
App({ onLaunch: function() { // Do something initial when launch. }, onShow: function() { // Do something when show. }, onHide: function() { // Do something when hide. }, globalData: 'I am global data' })App.prototype.getCurrentPage()
getCurrentPage() function user gets the instance of the current page.
We provide the global getApp() function to obtain mini program instances.
// other.js var appInstance = getApp() console.log(appInstance.globalData) // I am global dataNote:
App() must be registered in app.js, and multiple registrations cannot be made.
Do not call getCurrentPage() during onLaunch, the page has not yet been generated.
After obtaining the instance through getApp, do not call the life cycle function privately.
The above is the detailed content of Teach you to use the app() function to register a program (WeChat program development). For more information, please follow other related articles on the PHP Chinese website!