HomeDocs
Skip to main content

Register App (Object object)

Register a mini program. Accepts an Object parameter specifying the mini program's lifecycle callbacks, among other things.

The App() function must be called in app.js, and it must be called exactly once. Failure to do so may result in unexpected consequences.

Parameters

Object object

PropertyTypeDefaultRequiredDescriptionMinimum Version
onLaunchfunctionNoLifecycle callback - listens for mini program initialization.1.0.0
onShowfunctionNoLifecycle callback - listens for mini program startup or foreground switch.1.0.0
onHidefunctionNoLifecycle callback - listens for mini program background switch.1.0.0
onErrorfunctionNoError listening function.3.7.5
onPageNotFoundfunctionNoError listening function.4.0.0
Other propertiesanyNoDevelopers can add any functions or data variables to the Object parameter, which can be accessed using 'this'.1.0.0

onLaunch()

Triggered when the mini program initialization is complete. It is only triggered once globally. To obtain launch parameters, use dlt.getLaunchOptions.

onShow(Object object)

Triggered when the mini program starts or switches to the foreground.

onHide()

Triggered when the mini program switches to the background.

onPageNotFound(Object object)

Event for when the page to be opened does not exist in the mini program.

onError(String error)

Listen to mini program error events, such as script errors or API call errors.

Example Code

// app.js
App({
onLaunch() {
// Do something initial when launch.
},
onShow() {
// Do something when show.
},
onHide() {
// Do something when hide.
},
globalData: 'I am global data',
});

The entire mini program has only one App instance, which is shared across all pages. Developers can use the getApp method to obtain the globally unique App instance, access data on App, or call functions registered on App.

// xxx.js
const appInstance = getApp();
console.log(appInstance.globalData); // I am global data
Privacy agreementDeveloper agreementcontact us: developer_private.mi@dlightek.com © 2024 MiniApp. All Rights Reserved.