search
HomeWeChat AppletMini Program DevelopmentOverview of App() and Page() functions for practical development of WeChat mini programs

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 in the foreground from the background.


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:

Overview of App() and Page() functions for practical development of WeChat mini programs

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
Listening to page loading

Triggered when the page first starts loading. 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
Listen to the page display

Triggered when the page is displayed, such as page switching


onHide
Listening page hide

corresponds to onShow


onUnload
Listening page unloading

Call when redirectTo or navigateBack


onPullDownRefresh
Listen to user pull down

1) Need to be in config EnablePullDownRefresh is turned on in the window options.
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 for the initial data of the page. Revise. 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 homepage, the last The element is the current page.


2.4, case animation

Overview of App() and Page() functions for practical development of WeChat mini programs

3. Page stack

Overview of App() and Page() functions for practical development of WeChat mini programs

The framework maintains all current pages in the form of a stack. When a routing switch occurs, the page stack behaves as follows:

Overview of App() and Page() functions for practical development of WeChat mini programs

5. Page routing

Overview of App() and Page() functions for practical development of WeChat mini programs


For more articles related to the overview of App() and Page() functions in practical development of WeChat mini programs, please pay attention to 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment