


WeChat Mini Program Development Tutorial-App() and Page() Function Overview
1. App()
is used to register a small program. Called when the applet starts, and creates the applet until it is destroyed. It exists throughout the entire life cycle of the mini program. Obviously it is singleton and global. So,
1) can only be registered once in app.js.
2) You can obtain this unique small program singleton through getApp() anywhere in the code,
For example, var appInstance = getApp();
The parameter of App() is object type {}, which specifies the declaration cycle function of the applet.
onLaunch function
Listens for the initialization of the applet.
When the mini program initialization is completed, onLaunch will be triggered (only triggered once globally).
onShow function
Monitors the mini program display.
It will be triggered when the mini program is started or displayed from the background to the foreground.
onHide function
The listening applet is hidden.
It will be triggered when the mini program enters the background from the foreground.
The definition of the so-called front and backend is similar to an app on a mobile phone. For example, when you are not using WeChat, you enter the backend.
globalData object
Global data.
Code and log reference, as shown in the following animation:
##2. Page() function
After registering the mini program through App(), the framework starts to register the page. So do not call the getCurrentPage() method in onLaunch of App(), because the page has not been registered yet.
The same Page() also has a life cycle. After the page registration is completed, you can call the getCurrentPage() method in the page.js file to obtain the current page object.
2.1, the parameters of Page() are also of type Object.
onLoad Listen to page loading
Triggered when the page first starts to load. Will only be called once.
onReady Listen to the completion of the initial rendering of the page
Similar to html's onReady. Will only be called once.
onShow Monitoring page display
Triggered when the page is displayed, such as page switching
onHide Listening page hide
corresponds to onShow
onUnload Listen for page unloading
Called during redirectTo or navigateBack
onPullDownRefresh Listen to user pull down
1) You need to enable enablePullDownRefresh in the window option of config.
2) After processing the data refresh, wx.stopPullDownRefresh can stop the pull-down refresh of the current page.
onReachBottom
Handling function for page pull-down event
data
Initial data of the page
2.2, Page.prototype.setData()
Page’s function setData() is used to modify the initial data of the page. If the data is bound to the view layer wxml and displayed, the view layer will reflect the modification without refreshing.
For data modification, you can only use setData() and cannot modify it directly through this.data. Data size is limited to 1024 kb.
2.3, getCurrentPages()
, get the instance of the current page stack, given in the form of an array in the order of the stack, the first element is the home page , the last element is the current page.
2.4, case animation
##3. Page stack
The framework maintains all current pages in the form of a stack. When a routing switch occurs, the page stack behaves as follows:
Routing method | Page stack performance |
||||||||||||||||||
##New page is pushed into the stack | |||||||||||||||||||
Push a new page into the stack | |||||||||||||||||||
The current page is popped out of the stack, |
|||||||||||||||||||
The page continues to pop out of the stack until the target returns to the page,
|
|||||||||||||||||||
The current page is popped out, |
Routing method | Page after routing |
Page before routing |
Initialization |
|
|
onShow | onHide||
##onLoad, |
||
onShow | onUnload (Multi-level page return Each page will press Trigger onUnload sequentially) | |
Open for the first time onLoad, onshow; |
##More WeChat mini programs Development Tutorial - Overview of App() and Page() functions. For related articles, please pay attention to the PHP Chinese website! |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.