# App(Object object)

Register Weixin Mini Program.Accepts aObjectparameter that specifies the lifecycle callback of the Mini Program, etc.

App () must be called inapagejs. It must be called, and only once.Otherwise there will be unforeseen consequences.

# parameter

# Object object

attribute type Default values Required to fill in Introductions Minimum version
onLaunch function no Life Cycle Callback - Listening Weixin Mini Program Initial implementation.
onShow function no Life Cycle Callback -- Listens for Weixin Mini Program to start or cut foreground.
onHide function no Life Cycle Callback -- Listen to Weixin Mini Program cut background.
onError function no Error listening function.
onPageNotFound function no There is no listening function on the page. 1.9.90
onUnhandledRejection function no Unprocessed Promise rejection event listener function. 2.10.0
onThemeChange function no Listening System Theme Change 2.11.0
Other any no Developers can add arbitrary functions or data variables to theObjectparameter, and can access it withthis

For the definition of the front and back of the Weixin Mini Program and the running mechanism of the Mini Program, please refer to the running mechanism chapter.

# onLaunch(Object object)

Weixin Mini Program is triggered when the initialization is completed, and globally only once.Parameters can also be obtained using wx.getLaunchOptionsSync .

Parameter : Consistent with wx.getLaunchOptionsSync

# onShow(Object object)

Weixin Mini Program is started, or triggered when entering the foreground display from the background.You can also use wx.onAppShow binding listening.

Parameter : Consistent with wx.onAppShow

# onHide()

Weixin Mini Program Triggered when entering the background from the foreground.You can also use wx.onAppHide binding listening.

Parameter : Consistent with wx.onAppHide

# onError(String error)

Weixin Mini Program Triggered when a script error or API call error occurs.You can also use wx.onError binding listening.

Parameter : consistent with wx.onError

# onPageNotFound(Object object)

Start from base library version 1.9.90. Please remaining backward compatible.

Weixin Mini Program Triggered when the page to open does not exist.You can also use wx.onPageNotFound binding listening.Please refer to wx.onPageNotFound .

Parameter : Consistent with wx.onPageNotFound

Example code:

App({
  onPageNotFound(res) {
    wx.redirectTo({
      url: 'pages/...'
    }) // 如果是 tabbar 页面,请使用 wx.switchTab
  }
})

# onUnhandledRejection(Object object)

Start from base library version 2.10.0. Please remaining backward compatible.

Weixin Mini Program Triggered when there is an unprocessed Promise rejection.You can also use wx.onUnhandledRejection binding listeners.Please refer to wx.onUnhandledRejection .

Parameter : consistent with wx.onUnhandledRejection

# onThemeChange(Object object)

Start from base library version 2.11.0. Please remaining backward compatible.

Triggered when the system switches themes.You can also use wx.onThemeChange binding listening.

Parameter : consistent with wx.onThemeChange

# sample code

App({
  onLaunch (options) {
    // Do something initial when launch.
  },
  onShow (options) {
    // Do something when show.
  },
  onHide (options) {
    // Do something when hide.
  },
  onError (msg) {
    console.log(msg)
  },
  globalData: 'I am global data'
})