WeChat Open Platform

Mini Programs Documentation

App

App()

The App() function is used to register a Mini Program. When it receives an object parameter, it specifies the Mini Program's lifecycle functions, etc.

Object parameter descriptions:

Attribute Type Description When triggered
onLaunch Function Lifecycle function - monitors Mini Program initialization onLaunch will be triggered when a Mini Program has completed initialization (only triggered once globally)
onShow Function Lifecycle function - monitors Mini Program display onShow will be triggered when a Mini Program starts up, or enters the foreground from the background
onHide Function Lifecycle function - monitors Mini Program hiding onHide will be triggered when a Mini Program enters the background from the foreground
onError Function Error monitoring function onError will be triggered when a Mini Program script error occurs, or an api call is unsuccessful, and brings an error message
Other Any Developers can add any functions or data to the object parameters and use this to access them

Foreground and background definitions: When the user taps the top left corner to close WeChat, or presses the device's home button to leave WeChat, the Mini Program will not be removed directly but will enter the background. When WeChat is entered again or the Mini Program is opened again, the Mini Program will enter the foreground from the background. The following needs to be noted: a Mini Program will only really be removed if it enters the background for a certain amount of time, or too many system resources are being used.

Closing Mini Programs (support beginnig with common library version 1.1.0): When a user enters a Mini Program by scanning a QR Code or from shared access (scene value is 1007, 1008, 1011, or 1025), and exits when there is no Sticky on Top Mini Program situation, the Mini Program will be removed.

Sample code:

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

onLaunch and onShow parameters

Field Type Description
path String Opens Mini Program path
query Object Opens Mini Program query
scene Number Opens Mini Program scene value
shareTicket String shareTicket, refer to Get More Information on Forwarding for more details

Scene value Refer to for more details.

getApp()

We have provided a global getApp() function that can get Mini Program instances.

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

Note:

App() must be registered in app.js and multiple apps cannot be registered.

Do not call getApp() from a function within the App(), you can use this to get app instances.

Do not call getCurrentPage() when in onLaunch, this page has not been generated yet.

After using getApp() to get an instance, do not call the lifecycle function without authorization.