App(Object object)

Register the applet. Accept one Object Parameter that specifies the applet's lifecycle callback, etc.

App () must be in app.js Must be called and can only be called once. Otherwise, there will be unexpected consequences.

parameter

Object object
attribute type Default value Required explain Minimum version
onLaunch function no Initializing the applet -- listening to the callback.
onShow function no Life cycle callback -- listen to the applet to start or switch to the foreground.
onHide function no Life cycle callback -- monitor the applet and cut the background.
onError function no Error listener function.
onPageNotFound function no There is no listener function on the page. 1.9.90
onUnhandledRejection function no Unhandled promise denial event listener function. 2.10.0
onThemeChange function no Monitoring system theme changes 2.11.0
other any no Developers can add any function or data variable to Object In the parameter, use this Can be accessed

For the definition of applet background and applet operation mechanism, please refer to operating mechanism Chapter.

Sample code

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

onLaunch(Object object)

When the initialization of the applet is completed, the global trigger is only triggered once. Parameters can also be used wx.getLaunchOptionsSync obtain.

parameter : and wx.getLaunchOptionsSync agreement

onShow(Object object)

Triggered when the applet starts or enters the foreground display from the background. It can also be used wx.onAppShow Bind listening.

parameter : and wx.onAppShow agreement

onHide()

Triggered when the applet enters the background from the foreground. It can also be used wx.onAppHide Bind listening.

onError(String error)

Triggered when a script error or API call error occurs in the applet. It can also be used wx.onError Bind listening.

parameter And: wx.onError agreement

onPageNotFound(Object object)

Basic library 1.9.90 starts to support, low version needs to be done Compatible processing

Triggered when the page the applet wants to open does not exist. It can also be used wx.onPageNotFound Bind listening. Please refer to wx.onPageNotFound

parameter : and wx.onPageNotFound agreement

Example code:

 App ( {
   onPageNotFound ( res )  { wx . redirectTo ( { url :  'pages/...'
     } )  //For tabbar pages, use Wx. Switchtab
   }
 } )

onUnhandledRejection(Object object)

Basic library 2.10.0 starts to support, low version needs to be done Compatible processing

Triggered when an applet has an unhandled promise denial. It can also be used wx.onUnhandledRejection Bind listening. Please refer to wx.onUnhandledRejection

parameter : and wx.onUnhandledRejection agreement

onThemeChange(Object object)

Basic library 2.11.0 starts to support, low version needs to be done Compatible processing

Triggered when the system switches topics. It can also be used wx.onThemeChange Bind listening.

parameter : and wx.onThemeChange agreement