Registration procedure


App()


App() function is used to register a small program. Accepts an object parameter, which specifies the life cycle function of the applet, etc.

object parameter description:

QQ截图20170208095544.png

##Foreground and background definition: When the user Click the upper left corner to close, or press the home button of the device to leave WeChat. The mini program is not being destroyed, but enters the background. When you start WeChat again or open the mini program again, it will enter the foreground from the background.

Only when the mini program enters the background for a certain period of time, or the system resource usage is too high, will it be truly destroyed.

Sample code:

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

App.prototype.getCurrentPage()


getCurrentPage()Function user acquisition An instance of the current page.

getApp()


We provide the global

getApp() function, which can obtain the mini program instance.

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

Note:

App() must be registered in app.js, and multiple registrations cannot be made.

Do not call

getApp() in a function defined in App(). Use this to get the app instance.

Do not call

getCurrentPage() during onLaunch, the page has not yet been generated.

After obtaining the instance through getApp, do not call the life cycle function privately.