May 18, 2021 WeChat Mini Program Development Document
Register the small program. Accept an Object parameter that specifies lifecycle callbacks for small programs, and so on.
App() must be .js in the app, must be called, and can only be called once. Otherwise, there will be u unpredictable consequences.
Property | Type | The default | Required | Description | The lowest version |
---|---|---|---|---|---|
onLaunch | function | Whether | Lifecycle callbacks - Listen to the initialization of small programs. | ||
onShow | function | Whether | Lifecycle callbacks - Listen for small programs to start or cut the fore desk. | ||
onHide | function | Whether | Life cycle callbacks - Listen to small programs cut backstage. | ||
onError | function | Whether | Error listening function. | ||
onPageNotFound | function | Whether | The page does not have a listening function. | 1.9.90 | |
onUnhandledRejection | function | Whether | Unprocessed Promise rejects the event listening function. | 2.10.0 | |
onThemeChange | function | Whether | Listen for changes in system themes | 2.11.0 | |
Other | any | Whether |
Developers can add any function or data variable to
Object
parameter, which can
this
with this
|
Refer to the Run Mechanism section for the definition of the background before the small program and the operation mechanism of the small program.
App({
onLaunch (options) {
// Do something initial when launch.
},
onShow (options) {
// Do something when show.
},
onHide () {
// Do something when hide.
},
onError (msg) {
console.log(msg)
},
globalData: 'I am global data'
})
Triggered when the program initialization is complete, the global trigger is triggered only once. Parameters can also be obtained using wx.getLaunchOptionsSync.
Parameter: Consistent with wx.getLaunchOptionsSync
The small program starts, or is triggered when it enters the fore reception display from the background. You can also use wx.onAppShow to bind listening.
Parameter: Consistent with wx.onAppShow
The small program is triggered when the front desk enters the background. You can also use wx.onAppHide binding to listen.
Triggered when a script error occurs or an API call is reported. You can also use wx.onError to bind listening.
Parameter: Consistent with wx.onError
Base Library 1.9.90 starts to support, and low versions need to be compatible.
Triggered when the page to be opened by the small program does not exist. Y ou can also use wx.onPageNotFound to bind listening. Note that please refer to wx.onPageNotFound.
Parameter: Consistent with wx.onPageNotFound
Example code:
App({
onPageNotFound(res) {
wx.redirectTo({
url: 'pages/...'
}) // 如果是 tabbar 页面,请使用 wx.switchTab
}
})
Base library 2.10.0 starts to be supported, and low versions need to be compatible.
Triggered when a small program has an unprocessed Promise deny. Y ou can also use wx.onUnhandledRejection to bind listening. For more information, please refer to wx.onUnhandledRejection.
Parameter: Consistent with wx.onUnhandledRejection
Base library 2.11.0 starts to support, and low versions need to be compatible.
Triggered when the system switches themes. You can also use wx.onThemeChange binding to listen.
Parameter: Consistent with wx.onThemeChange
Gets a globally unique app instance to the small program.
Property | Type | The default | Required | Description | The lowest version |
---|---|---|---|---|---|
allowDefault | boolean | false | Whether |
Returns
App
implementation when the app is not defined. W
hen an app is called, the properties defined in the default implementation are overwritten and merged into the app.
Typically used for separate subcontracts
|
2.2.4 |
// other.js
var appInstance = getApp()
console.log(appInstance.globalData) // I am global data