目录

App

Each atomic service calls App() for only one time in app.js to register ASCF, bind the lifecycle callback, and add a listener for errors. The object parameters allow you to define lifecycle callback, global data, global methods, and the like.

The following table lists the related parameters.

Name

Type

Mandatory

Description

onLaunch

function

No

Listens for the launch of ASCF. This event is triggered only once when ASCF is initialized. The parameter is the same as that of has.getLaunchOptionsSync.

onShow

function

No

Listens for the display of ASCF. This event is triggered after ASCF is loaded for the first time or when ASCF is switched to the foreground. Alternatively, has.onAppShow can be used to add a listener. The parameter is the same as that of has.onAppShow.

onHide

function

No

Listens for the hiding of ASCF. This event is triggered when ASCF is switched to the background. Alternatively, has.onAppHide can be used to add a listener.

onError

function

No

Listens for unprocessed errors. Alternatively, has.onError can be used to bind a listener. The parameter is the same as that of has.onError.

onPageNotFound

function

No

Listens for functions that do not exist on the page.

onLazyLoadError

function

No

Listens for failure events of asynchronous component loading.

Since: 1.0.8

Others

any

No

Adds any function or data variable to a parameter of the Object type and uses this to access the parameter.

The sample code snippet is as follows:

// app.js
App({
  onLaunch(options) {
    // Triggered when ASCF is loaded.
  },
  onShow(options) {
    // Triggered when ASCF is displayed.
  },
  onHide() {
    // Triggered when ASCF is hidden.
  },
  onError(msg) {
    console.log(msg)
  },
  globalData: 'global data'
})

The entire ASCF shares one globally unique App instance, which can be obtained by using the getApp method.

// xxx.js
const app = getApp()
console.log(app.globalData) // global data