App(Object object)

Sign up for the Mini Program. Take one. Object Parameter that specifies the lifecycle callbacks of Mini Programs, and so on.

App() Must be app.js Must be called and can be called only once. Or there will be unforeseen consequences.

parameter

Object object

attribute type Default value Required Introductions Minimum version
onLaunch function no Lifecycle callbackMonitor Mini Program initialization.
onShow function no Lifecycle callbackMonitor Mini Program start or cut the foreground.
onHide function no Lifecycle callbackListen to the Mini Program cut background.
onError function no Error listener.
onPageNotFound function no There is no listener function on the page. 1.9.90
onUnhandledRejection function no Untreated Promise Rejects event listener functions. 2.10.0
onThemeChange function no Monitor subject changes 2.11.0
Other any no Developers can add any function or data variable to Object Parameter, with the this Can access

For the definition of Mini Programs and the mechanism for running Mini Programs, see[Operation mechanism]((runtime/Operation-mechanism).

sample code

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

onLaunch(Object object)

Mini Program initialization is completed when the trigger, the global trigger only once. Parameters can also be used wx.getLaunchOptionsSync Get.

parameter: With wx.getLaunchOptionsSync Agreement

onShow(Object object)

Mini Program start, or from the background into the foreground triggered when the display. You can also use wx.onAppShow Bind listening.

parameter: With wx.onAppShow Agreement

onHide()

Triggered when the Mini Program enters the background from the foreground. You can also use wx.onAppHide Bind listening.

onError(String error)

Script error or API Triggered on an error call. You can also use wx.onError Bind listening.

parameter: With wx.onError Agreement

onPageNotFound(Object object)

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

Triggered when the page the Mini Program is trying to open does not exist. You can also use wx.onPageNotFound Bind listening. Attention please refer to wx.onPageNotFound

parameter: With wx.onPageNotFound Agreement

Sample code:

App({
  onPageNotFound(res) {
    wx.redirectTo({
      url: 'pages/...'
    }) // If it were Tabs Page, use the wx.switchTab
  }
})

onUnhandledRejection(Object object)

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

Mini Programs have unhandled Promise Triggered when rejected. You can also use wx.onUnhandledRejection Bind listening. Attention please refer to wx.onUnhandledRejection

parameter: With wx.onUnhandledRejection Agreement

onThemeChange(Object object)

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

Triggered when the system switches topics. You can also use wx.onThemeChange Bind listening.

parameter: With wx.onThemeChange Agreement