APP
App(Object object)
Register a mini program and accept an Object parameter, which specifies the life cycle callback of the mini program, etc.
App() must be called in app.js, and must be called and can only be called once, otherwise there will be unexpected consequences.
Parameter
Object object
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| onLaunch | function | - | No | Life cycle callback - listen for mini program initialization |
| onShow | function | - | No | Life cycle callback - listen for mini program startup or switching to the foreground |
| onHide | function | - | No | Life cycle callback - listen for mini program switching to the background |
| onError | function | - | No | Error listening function |
| onPageNotFound | function | - | No | Page does not exist listening function |
| onUnhandledRejection | function | - | No | Unhandled Promise rejection event listening function |
| onThemeChange | function | - | No | Listen for system theme changes |
| Other | any | - | No | Developers can add any function or data variable to the Object parameter, which can be accessed using this |
For the definition of the front and back ends of mini programs and the operation mechanism of mini programs, please refer to Applet Operation Mechanism section
onLaunch(Object object)
Triggered when the mini program is initialized. It is triggered only once globally. The parameters can also be obtained using wx.getLaunchOptionsSync obtain.
Parameters: Same as wx.getLaunchOptionsSync same.
onShow(Object object)
It is triggered when the applet is started, or when it enters the foreground from the background. You can also use wx.onAppShow bind monitoring.
Parameters: Same as wx.onAppShow same.
onHide()
It is triggered when the applet enters the background from the foreground. You can also use wx.onAppHide bind monitoring.
onError(String error)
It is triggered when a script error or API call error occurs in the applet. You can also use wx.onError bind monitoring.
Parameters: Same as wx.onError same.
onPageNotFound(Object object)
It is triggered when the page to be opened by the applet does not exist. You can also use wx.onPageNotFound Bind monitoring. For precautions, please refer to wx.onPageNotFound
Parameters: Same as wx.onPageNotFound same.
Sample code:
App({
onPageNotFound(res) {
wx.redirectTo({
url: 'pages/...'
}) // If it were Tabs Page, use the wx.switchTab
}
})onUnhandledRejection(Object object)
It is triggered when there is an unhandled Promise rejection in the applet. You can also use wx.onUnhandledRejection Bind monitoring. For precautions, please refer to wx.onUnhandledRejection
Parameters: Same as wx.onUnhandledRejection same.
onThemeChange(Object object)
It is triggered when the system switches the theme. You can also use wx.onThemeChange bind monitoring.
Parameters: Same as wx.onThemeChange same.
Sample code:
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'
})AppObject getApp(Object object)
Get the globally unique App instance of the applet.
Parameter
Object object
| Properties | Type | Default value | Required | Description |
|---|---|---|---|---|
| allowDefault | boolean | false | No | Return the default implementation when App is not defined. When App is called, the properties defined in the default implementation will be overwritten and merged into App. It is generally used for Independent subpackage |
Sample code:
// other.js
var appInstance = getApp()
console.log(appInstance.globalData) // I am global dataNotes
Do not call getApp() in the function defined in App() or before calling App. You can get the app instance by using this;
After getting the instance through getApp(), do not call the life cycle function privately.