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

Mercedes-Benz G-Class launches two new models

The 2016 Guangzhou Auto Show has officially kicke...

How to plan a successful and beautiful offline event?

As an operator , event promotion ( event operatio...

A collection of the latest mainstream Android market resources in 2019!

7 major channels to help your APP recommend itsel...

Primary school students’ “Buddhist style of skewers”, do you get it?

Review expert: Zhu Guangsi, member of Beijing Sci...

Several existing problems and solutions for App Store search

Apple's volatility has only increased recentl...

Do you think this belly button is a bit scary?

There are many strange phobias in the world, such...

60 technical experiences summarized in daily Android development

[[181259]] 1. All Activities can inherit from Bas...

Using deep neural networks to solve the problem of NER named entity recognition

This article is organized as follows: What is Nam...

90% of brand marketing doesn’t even have common sense

In the face of a rapidly changing era of traffic,...