Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small programs Register small programs


May 17, 2021 WeChat Mini Program Development Document



Register the small program

Each gadget needs to call the App method in app.js to register the instance of the program, bind lifecycle callback functions, error listening, and the page does not have a listening function.

Refer to the app reference documentation for detailed parameter meaning and use.

// app.js
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'
})

The entire gadget has only one app instance and is shared across all pages. Developers can get globally unique app instances through the getApp method, get data on the app, or call the functions that the developer registered on the app.

// xxx.js
const appInstance = getApp()
console.log(appInstance.globalData) // I am global data