App()is used to register a Mini Program.
App()accepts an Object attribute to configure the lifecycle of the Mini Program.
You can call App()only in the
app.jsfile and only for once.
|onLaunch||Function||Lifecycle callback: listens to the initialization of the Mini Program||Triggered when the initialization of the Mini Program completes. Triggered only for once during the lifecycle of the Mini Program.|
|onShow||Function||Lifecycle callback: listens to the display of the Mini Program||Triggered when the Mini Program is launched or triggered when the display is switched to the foreground from the background.|
|onHide||Function||Lifecycle callback: listens to hide operations of the Mini Program||Triggered when the Mini Program is switched to the background from the foreground.|
|onShareAppMessage||Function||Configures global sharing|
Definitions of the foreground and background of a Mini Program:
- When a user taps the close button in the upper-right corner to close a Mini Program or presses the Home button on the device to leave Alipay, the Mini Program is not terminated but runs in the background.
- When the user re-opens Alipay or the Mini Program, the Mini Program will run from the background to the foreground.
- A Mini Program is terminated only after it runs in the background for a specific period of time or it occupies excessive system resources.
The following table describes the object attributes.
|query||Object||The current query object of the Mini Program. This object is resolved from the “query” field for Mini Program startup.|
|path||String||The current page address of the Mini Program. This page address is resolved from the “page” field for Mini Program startup. If the “page” field is not specified for Mini Program startup, the path corresponds to the homepage.|
|referrerInfo||Object||The source information.|
For example, the schema URL for starting a Mini Program is shown as follows:
The following code snippet shows the resolution results of the “query” and “path” attributes:
query = decodeURIComponent('number%3D1');
path = decodeURIComponent('x%2Fy%2Fz');
- When a Mini Program is started for the first time, you can use the
onLaunchmethod to obtain the values of the
- When the Mini Program is opened by using a schema URL, you can use the
onShowmethod to obtain the values of the
// Open for the first time.
// Re-opened by schema in the background.
The following table describes the sub attributes of the
|appId||String||From the source Mini Program.|
|sourceServiceId||String||The source plug-in, which is visible in running mode.||1.11.0|
|extraData||Object||The data that is transferred from the source Mini Program.|
- Do not call a method that manages pages, such as
navigateTo, in the
- Do not call the
getCurrentPages()method in the
onLaunchevent, because the Page objects have not been generated.
onHide() event is triggered when the Mini Program is switched to the background from the foreground.
// This event is triggered when the Mini Program is switched to the background.
// This event is triggered when an execution error occurs in the Mini Program.
This event is triggered to configure global sharing. If
page.onShareAppMessage is not configured for a page, global sharing configurations apply when the page is being shared. For more information, see Sharing.
You can use
globalData to configure global data in