Mini Program Framework API App and getApp Parameters

Mini Program Framework API App and getApp Parameters

Dongguan mini program development framework interface App and getApp parameters, register mini program. Accepts an Object parameter, which specifies the life cycle callback of the applet, etc. App() must be called in app.js and must be called and can only be called once. Otherwise there will be unpredictable consequences.

App(Object object)

parameter

Object object
property type default value Required illustrate
onLaunch Function no Life cycle callback - listen for mini program initialization.
onShow Function no Lifecycle callback - listen for the mini program to start or switch to the foreground.
onHide Function no Lifecycle callback - listen for the applet to switch to the background.
onError Function no Error listening function.
onPageNotFound Function no The page does not have a listener function.
other any no Developers can add any functions or data variables to the Object parameter and use this to access them.

For the definition of the front-end and back-end of the mini program and the operation mechanism of the mini program, please refer to the operation mechanism section.

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)

Triggered when the mini program is initialized, and only triggered once globally. Parameters can also be obtained using wx.getLaunchOptionsSync.

Parameters: Same as wx.getLaunchOptionsSync

onShow(Object object)

Triggered when the applet is started or enters the foreground from the background. You can also use wx.onAppShow to bind the listener.

Parameters: Same as wx.onAppShow

onHide()

Triggered when the applet enters the background from the foreground. You can also use wx.onAppHide to bind the listener.

onError(String error)

Triggered when a script error or API call error occurs in the mini program. You can also use wx.onError to bind the listener.

Parameters: Same as wx.onError

onPageNotFound(Object object)

Support is available starting from the basic library 1.9.90, and lower versions need to be compatible.

Triggered when the page that the applet wants to open does not exist. You can also use wx.onPageNotFound to bind the listener. Please refer to wx.onPageNotFound for precautions.

Parameters: Same as wx.onPageNotFound

Sample code:

 App({
  onPageNotFound(res) {
    wx.redirectTo({
      url: 'pages/...'
    }) // If it is a tabbar page, please use wx.switchTab
  }})

AppObject getApp(Object object)

Get the globally unique App instance of the Mini Program.

parameter

Object object
property type default value Required illustrate Minimum version
allowDefault boolean false no The default implementation is returned when App is not defined. When the App is called, the properties defined in the default implementation will be overwritten and merged into the App. Generally used for independent subcontracting 2.2.4

Sample Code

 // other.jsvar appInstance = getApp()console.log(appInstance.globalData) // I am global data

Notice

  • Do not call getApp() in a function defined in App() or before calling App. Use this to get the app instance.
  • After obtaining the instance through getApp(), do not call lifecycle functions privately.

<<:  Complete review: How to write an attractive information flow idea?

>>:  How to use Tencent’s information flow advertising to win the Double 11 battle for traffic?

Recommend

Don’t worry! Big companies won’t “steal” your entrepreneurial idea

[[161042]] A common question startups ask is, “Wh...

Property management should be free in the Internet age

When free becomes a "business card" in ...

Don't sit in the massage chairs in the mall, they are terrible!

Have you ever encountered this thing in a shoppin...

It's rude to be hotter! Is it really normal to be so hot this summer?

I don't know if you have noticed that every y...

Zhejiang Telecom Large Bandwidth Rental

Zhejiang Telecom large bandwidth rental, server l...

iOS alternative memory management

[[129290]] OS memory management is a commonplace ...