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

8 steps and 5 stages to build a private community SOP from 0 to 1

There is more and more consensus on doing private...

What are the elements for configuring BGP line server for gaming website?

There are many server requirements for game websi...

How to use information flow to follow hot topics?

I believe everyone knows the importance of follow...

How much does it cost to be an agent for Qian'an's online recharge mini program?

How much does it cost to be an agent for an onlin...

Self-management book list for efficient life (all 10 books)

Introduction This set of books includes 10 volume...

How to make an event planning plan? Event planning skills!

Activities are a means to quickly achieve specifi...

A new way to innovate e-commerce - earn money by buying in groups!

When I was talking with a friend last week, I cam...

How should short video advertisements such as Douyin and Kuaishou be placed?

1. Take a holistic view and use data to understan...

Tencent’s advertising and gaming industry buying report for June!

This article shares with you Tencent’s advertisin...

A must-read for APP promotion, complete promotion data analysis in 3 steps!

When I was reporting business dynamics to the bos...