Home  >  Article  >  WeChat Applet  >  Teach you to use the app() function to register a program (WeChat program development)

Teach you to use the app() function to register a program (WeChat program development)

Y2J
Y2JOriginal
2017-04-22 11:53:192224browse

This article mainly introduces the relevant information of the registration process of the WeChat Mini Program Tutorial. Friends who need it can refer to the

series of articles:

Modularization of the WeChat Mini Program Tutorial

Registration page of WeChat mini program tutorial

Registration process of WeChat mini program tutorial

App()

App() function Let’s register for a mini program. Accepts an object parameter, which specifies the life cycle function of the applet, etc.

object parameter description:

##onLaunchFunctionLife cycle function--Listening applet initializationWhen the mini program initialization is completed, onLaunch will be triggered (globally only triggered once) onShowFunctionLife cycle function- - Listening to the mini program displayWhen the mini program starts, or enters the foreground display from the background, onShowonHideFunctionOthers
Attribute Type Description Trigger timing
# will be triggered ##Life cycle function--Monitoring applet hiding When the applet enters the background from the foreground, onHide will be triggered
Any Developers can add any function or data to the Object parameter, and use this to access
Foreground and background definitions :

When the user clicks on the upper left corner to close, or presses the home button of the device to leave WeChat, the mini program is not being destroyed, but enters the background; when the user starts WeChat again or opens 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 gets the instance of the current page.


getApp()

We provide the global getApp() function to obtain 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 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.

The above is the detailed content of Teach you to use the app() function to register a program (WeChat 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