All Products
Search
Document Center

Mobile Platform as a Service:app.js registering Mini Program

Last Updated:Feb 02, 2021

App(object: Object)

  • 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.js file and only for once.

Descriptions of object attributes

Attribute Type Description Trigger condition
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.
onError Function Listens to errors in the Mini Program Triggered when a JavaScript error occurs in the Mini Program.
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.

onLaunch(object: Object) and onShow(object: Object)

The following table describes the object attributes.

Attribute Type Description
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:

  1. alipays://platformapi/startapp?appId=1999&query=number%3D1&page=x%2Fy%2Fz

The following code snippet shows the resolution results of the “query” and “path” attributes:

  1. query = decodeURIComponent('number%3D1');
  2. // number=1
  3. path = decodeURIComponent('x%2Fy%2Fz');
  4. // x/y/z
  • When a Mini Program is started for the first time, you can use the onLaunch method to obtain the values of the query and path attributes.
  • When the Mini Program is opened by using a schema URL, you can use the onShow method to obtain the values of the query and path attributes.
  1. App({
  2. onLaunch(options) {
  3. // Open for the first time.
  4. console.log(options.query);
  5. // {number:1}
  6. console.log(options.path);
  7. // x/y/z
  8. },
  9. onShow(options) {
  10. // Re-opened by schema in the background.
  11. console.log(options.query);
  12. // {number:1}
  13. console.log(options.path);
  14. // x/y/z
  15. },
  16. });

The following table describes the sub attributes of the referrerInfo attribute.

Attribute Type Description Compatibility
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.
Notes:
  • Do not call a method that manages pages, such as redirectTo or navigateTo, in the onShow event.
  • Do not call the getCurrentPages() method in the onLaunch event, because the Page objects have not been generated.

onHide()

The onHide() event is triggered when the Mini Program is switched to the background from the foreground.

Code sample:

  1. App({
  2. onHide() {
  3. // This event is triggered when the Mini Program is switched to the background.
  4. console.log('app hide');
  5. },
  6. });

onError(error: String)

This event is triggered when a JavaScript error occurs or an API call fails in the Mini Program.

  1. App({
  2. onError(error) {
  3. // This event is triggered when an execution error occurs in the Mini Program.
  4. console.log(error);
  5. },
  6. });

onShareAppMessage(object: Object)

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.

globalData

You can use globalData to configure global data in App().

Code sample:

  1. // app.js
  2. App({
  3. globalData: 1
  4. });