Register Mini Program
App(Object)
App()
is used to register the Mini Program, accepts an object as the parameter to configure the lifecycle of Mini Program. App()
should be called in app.js and only be called once.
Object Parameter Description
Property | Type | Description | Trigger |
onLaunch | Function | Listening to Mini Program initialization. | On completion of Mini Program initialization, invoked only once. |
onShow | Function | Listening to Mini Program showing. | On startup of Mini Program or switching to foreground from background. |
onHide | Function | Listening to Mini Program hiding. | On switching Mini Program from foreground to background. |
onError | Function | Listening to Mini Program error. | On js error of the Mini Program. |
onUnhandledRejection | Function | Listen for the unhandledrejectionevent. | Triggered when a JavaScript |
Foreground/background definition:
- When the user leaves mobile app with the close button at upper-right corner or the device Home button, the Mini Program is not directly destroyed but switched to the background.
- When mobile app is started or the Mini Program is opened again, it is switched to the foreground from the background.
- Only when the Mini Program stays in background for a certain time or occupies too many system resources, it is destroyed.
onLaunch/onShow Options Parameter Description
Property | Type | Description |
query | Object | Current Mini Program query, parsed from the query field in the startup parameter. |
path | String | Current Mini Program page address, parsed from the page field in the startup parameter, home page by default when page is ignored. |
referrerInfo | Object | Source information. |
- This parameter can be obtained from the
onLaunch
method upon the first-time Mini Program startup
- The parameter can also be obtained from the
onShow
method when the Mini Program in background is reopened with schema.
App({
onLaunch(options) {
// first opening
console.log(options.query);
// {number:1}
},
onShow(options) {
// reopening with schema from background
console.log(options.query);
// {number:1}
},
})
referrerInfo attribute description
Property | Type | Description | Compatibility |
appId | string | Source Mini Program. | |
sourceServiceId | String | Source plug-in, visible in the plug-in running mode. | 1.11.0 |
extraData | Object | Data transferred from the source Mini Program. |
Notes:
- Do not operate page stack like redirectTo/navigateTo on the onShow.
- The basic library version used in AppContainer currently is 1.14.2.
onHide()
The onHide()
method will be triggered when Mini Program changes to background from foreground.
Sample code
App({
onHide() {
// when changes to background
console.log('app hide');
},
});
onError()
The onError()
method will be triggered when script error happens.
Sample code
App({
onError(error) {
// the Mini Program script error happens
console.log(error);
},
});
onUnhandledRejection()
The onUnhandledRejection() method will be triggered when a JavaScript Promise
that has no rejection handler is rejected.
Sample code
App({
onUnhandledRejection(res) {
// A JavaScript Promise that has no rejection handler is rejected.
console.log(res.reason, res.promise);
//res.reason describes the rejection reason and res.promise describes the rejected Promise.
},
});
Global Data
Global data can be configured in App()
. Other pages can get and modify the global data directly.
Sample code
// app.js
App({
globalData: 1
});
FAQ
Q: Can Mini Program be closed in app.js?
A: No, Mini Program can only be closed by clicking close button in the top right corner.