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

AttributeTypeDescriptionTrigger
onLaunchFunctionListening to Mini Program initializationOn completion of Mini Program initialization, invoked only once
onShowFunctionListening to Mini Program showingOn startup of Mini Program or swithing to foreground from background
onHideFunctionListening to Mini Program hidingOn switching Mini Program from foreground to background
onErrorFunctionListening to Mini Program errorOn js error of the Mini Program

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

AttributeTypeDescription
queryObjectCurrent Mini Program query, parsed from the query field in the startup parameter
pathStringCurrent Mini Program page address, parsed from the page field in the startup parameter, home page by default when page is ignored
referrerInfoObjectSource 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.
copy
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

AttributeTypeDescriptionCompatibility
appIdstringSource Mini Program
sourceServiceIdStringSource plug-in, visible in the plug-in running mode1.11.0
extraDataObjectData transferred from the source Mini Program

Note:

  • 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

copy
App({
  onHide() {
    // when changes to background
    console.log('app hide');
  },
});

onError()

The onError() method will be triggered when script error happens.

Sample code

copy
App({
  onError(error) {
    // the Mini Program script error happens
    console.log(error);
  },
});

Global Data

Global data can be configured in App(). Other pages can get and modify the global data directly.

Sample code

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

FAQ

Q: Can Mini Program be closed in app.js?

No, Mini Program can only be closed by clicking close button in the top right corner.