Home >WeChat Applet >Mini Program Development >APP() function for small program development

APP() function for small program development

Y2J
Y2JOriginal
2017-05-17 16:47:003110browse

App()

The 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:

APP() function for small program development

Foreground and background definition: When the user clicks on the upper left corner to close, or presses the device Home button to leave WeChat, the mini program does not It is destroyed directly and enters the background; when you enter WeChat again or open the mini program again, it will enter the foreground from the background.

Close the mini program (supported since public library version 1.1.0): When the user enters the mini program from the scanning, sharing and other entrances (scene values ​​are 1007, 1008, 1011, 1025), and there is no pinned mini program In the case of a program, exit, the applet will be destroyed.

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(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, onShow parameters

APP() function for small program development

Scene values ​​See details.

getApp()

We provide the global getApp() function to obtain the mini program instance.

// other.jsvar 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.

【Related Recommendations】

1. Special Recommendation: "php Programmer Toolbox" V0.1 version download

2. Complete source code download of WeChat Mini Program

3. WeChat Mini Program demo: Zhihu Daily

The above is the detailed content of APP() function for small program development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn