Register a Mini Program in app.js

更新时间:
复制 MD 格式

App(object: Object)

  • The App() method registers a Mini Program. It accepts an Object parameter to configure the Mini Program's lifecycle and other settings.

  • You must call the App() method exactly once in app.js.

Object property descriptions

Property

Type

Description

Trigger condition

onLaunch

Function

Lifecycle callback: listens for Mini Program initialization.

Triggered when the Mini Program finishes initialization. This is triggered only once globally.

onShow

Function

Lifecycle callback: listens for when the Mini Program is displayed.

Triggered when the Mini Program starts or is brought from the background to the foreground.

onHide

Function

Lifecycle callback: listens for when the Mini Program is hidden.

Triggered when the Mini Program moves from the foreground to the background.

onError

Function

Listens for Mini Program errors.

Triggered when a JavaScript error occurs in the Mini Program.

onShareAppMessage

Function

Global sharing configuration.

-

Foreground/background definition:

  • When a user taps the close button in the upper-right corner or presses the device's Home button to leave Alipay, the Mini Program is not immediately destroyed. Instead, it enters the background.

  • When the user returns to Alipay or reopens the Mini Program, the Mini Program moves from the background to the foreground.

  • The Mini Program is permanently destroyed only if it has been in the background for a certain period or if it consumes too many system resources.

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

Object property descriptions:

Property

Type

Description

query

Object

The query of the current Mini Program. It is parsed from the query field of the startup parameters.

path

String

The page address of the current Mini Program. It is parsed from the page field of the startup parameters. If the page field is omitted, it defaults to the home page.

referrerInfo

Object

Source information.

For example, the schema URL to start the Mini Program is as follows:

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

The parameters are parsed as follows:

query = decodeURIComponent('number%3D1');
// number=1
path = decodeURIComponent('x%2Fy%2Fz');
// x/y/z
  • When the Mini Program starts for the first time, you can retrieve the values of the query and path properties from the onLaunch method.

  • If the Mini Program is opened from the background using a schema, you can also obtain the values of the query and path properties from the onShow method.

App({
  onLaunch(options) {
    // First launch
    console.log(options.query);
    // {number:1}
    console.log(options.path);
    // x/y/z
  },
  onShow(options) {
    // Reopened from the background by a schema
    console.log(options.query);
    // {number:1}
    console.log(options.path);
    // x/y/z
  },
});

referrerInfo sub-property descriptions:

Property

Type

Description

Compatibility

appId

String

Source Mini Program.

-

sourceServiceId

String

Source plugin. This is visible when the Mini Program is running in plugin mode.

1.11.0

extraData

Object

Data passed from the source Mini Program.

-
Note

  • Do not perform page stack operations such as redirectTo or navigateTo in onShow.

  • Do not call <a baseurl="t2125955_v1_0_0.xdita" data-node="3196036" data-root="72221" data-tag="xref" href="t2125966.xdita#topic-2125966" id="a_ern_pqh_rqk">getCurrentPages()</a> in onLaunch because the page has not been generated yet.

onHide()

The onHide() method is triggered when the Mini Program moves from the foreground to the background.

Sample code:

App({
  onHide() {
    // When entering the background
    console.log('app hide');
  },
});

onError(error: String)

This method is triggered when a script error or an API call error occurs.

App({
  onError(error) {
    // When a Mini Program execution error occurs
    console.log(error);
  },
});

onShareAppMessage(object: Object)

This method provides global sharing configuration. When page.onShareAppMessage is not set for a page, calling the share function executes the global sharing settings. For more information, see Share.

Global data

You can set global data in App() using the globalData property.

Code example:

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