App(Object object)
Sign up for the Mini Program. Take one. Object Parameter that specifies the lifecycle callbacks of Mini Programs, and so on.
App() Must be app.js Must be called and can be called only once. Or there will be unforeseen consequences.
parameter
Object object
|
| function | - | no | Lifecycle callback--Monitor Mini Program initialization. |
| function | - | no | Lifecycle callback--Monitor Mini Program start or swicth to the frontend. |
| function | - | no | Lifecycle callback--Monitor Mini Program swicth to the backend. |
| function | - | no | Error listener. |
| function | - | no | There is no listener function on the page. |
| function | - | no | Untreated Promise Rejects event listener functions. |
| function | - | no | Monitor subject changes |
Other | any | - | no | Developers can add any function or data variable to Object Parameter, with the this Can access |
For the definition of Mini Programs and the mechanism for running Mini Programs, see Operation mechanism.
onLaunch(Object object)
Mini Program initialization is completed when the trigger, the global trigger only once. Parameters can also be used wx.getLaunchOptionsSync Get. onShow(Object object)
Mini Program start, or from the background into the foreground triggered when the display. You can also use wx.onAppShow Bind listening. onHide()
Triggered when the Mini Program enters the background from the foreground. You can also use wx.onAppHide Bind listening. onError(String error)
Script error or API Triggered on an error call. You can also use wx.onError Bind listening. onPageNotFound(Object object)
Triggered when the page the Mini Program is trying to open does not exist. You can also use wx.onPageNotFound Bind listening. Attention please refer to wx.onPageNotFound. Sample code:
App({
onPageNotFound(res) {
wx.redirectTo({
url: 'pages/...'
})
}
})
onUnhandledRejection(Object object)
onThemeChange(Object object)
Triggered when the system switches topics. You can also use wx.onThemeChange Bind listening. The translations are provided by WeChat Translation and are for reference only. In case of any inconsistency and discrepancy between the Chinese version and the English version, the Chinese version shall prevail.Incorrect translation.
Sample code:
App({
onLaunch (options) {
},
onShow (options) {
},
onHide () {
},
onError (msg) {
console.log(msg)
},
globalData: 'I am global data'
})
AppObject getApp(Object object)
Gets the Mini Programs globally unique App Instance.
parameter
Object object
|
allowDefault | boolean | false | no | in App Returns the default implementation when undefined. When the app is invoked, properties defined in the default implementation are overridden and incorporated into the app. Generally usedIndependent subcontracting |
Sample code
var appInstance = getApp()
console.log(appInstance.globalData)
Be careful
Don't define App() Or call the App Precall getApp() , using this You can ge it. app Instance;
adopt getApp() Do not privately call the lifecycle function after obtaining the instance.
Was this page helpful?