当前位置:网站首页>[applet practice series] Introduction to the registration life cycle of the applet framework page
[applet practice series] Introduction to the registration life cycle of the applet framework page
2022-06-30 11:15:00 【Hua Weiyun】
author : Half body wind and snow
Previous section : What is wechat applet
@[TOC]( Applet framework Page registration Life cycle Introduce )
One 、MINA frame
The running environment of small programs is divided into Render layer and logic layer , They are 2 Thread management : The rendering layer interface uses WebView Thread rendering ; Logic layer adopts JsCore Thread running JS Script . A small program has multiple interfaces , So there are multiple render layers WebView Threads , The communication between these two threads will be via wechat client ( We will also use Native Wechat client ) Make a transfer . The communication model of the applet is shown in the figure below :
Applet MINA The framework is close to the original App Running speed of . In fact, what he actually uses is the present IT The most respected in the world MVVM Pattern .view Layer corresponding render layer ,model Layer corresponds to logical layer ,viewmodel Layer correspondence native layer .
native The layer mainly does two things : Data binding and event listening . The logic layer sends network requests through Native forward .
1-1、 Render layer ( View layer View)
WXML Templates and WXSS Styles work in the render layer ,.wxml Used to describe the page structure , and .wxss Used to describe page styles .
The view layer presents data in a given style and feeds back events to the logic layer , And data presentation is done by components . Components (Component) It's the basic component of a view .
1-2、 Logic layer (APP Service)
JS Working at the logical level . Data is processed at the logical layer and sent to the view layer , At the same time, accept the event request sent back by the view layer .
Wechat team is right JavaScript Some optimizations have been made , In order to develop small programs more efficiently , These optimizations include :
- increase app Method to register a program , increase page Method to register the page .
- Provide a wealth of API Interface .
- The scope of the page is relatively independent , And has the ability of modularization .
But here's the thing , The logic layer of the applet consists of js To write , But it's not running in a browser , therefore JavaScript stay Web Some of the abilities in cannot be used , such as Dom、Window etc. .
1-3、setData function
On the page page() in , We use setData Function to send data from the logical layer to the view layer , At the same time change the corresponding this.data Value . There is a size limit for single set data , No more than 1024KB, Avoid setting too much data at once .
1-4、 Example
take home.wxml The contents of the file are modified to the following code :
<view bindtap="clickOn">{{content}}</view>
there bindtap Is a click event ,content It is the content to be displayed .
stay home.js File add definition content data , And click on the corresponding event :
Page({ data:{ content: "Hello Word" }, clickOn(){ console.log(" Click event "); this.setData({ content: "hello" }) }})Now run the project , The initial normalized data will be displayed :

Then click on the text content area , The text will become what we modified Hello:
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-zA13f9Z2-1655998408024)(/Users/tiger/Library/Application%20Support/typora-user-images/image-20220623232130414.png)]](https://img-blog.csdnimg.cn/9af84f11a973450289d4375168a62b65.png)
About the framework of the applet , I'll introduce it here first , More about , Let's go into Applet framework Study , The official documents explain things in detail .
Two 、 Interface rendering
2-1、 How the interface is rendered ?
First , We need to know ,wxml Equivalent to a tree DOM Trees , You can also use a JS Object to simulate ( fictitious DOM)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-3JXRsdzh-1655998408025)( structure dom Trees .png)]](https://img-blog.csdnimg.cn/3071688c790d43f5856933822dd331e3.png)
2-2、 If the data changes , How is it rendered ?
- Will create a new JS object
- At this point, you can compare the two JS The object gets the changed part
- Then apply this difference to the original Dom on the tree
- To update UI Purpose , This is it. " Data driven " Principle
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-4wuWgELq-1655998408026)(js data diff to update .png)]](https://img-blog.csdnimg.cn/c58495845a5e4b2f87a89b5f0c6d7bcd.png)
3、 ... and 、 Programs and pages
The applet contains a description of the overall program app And a number of page.
How the wechat client starts the applet ?
Wechat client before opening the applet , Will download the entire applet code package to the local . Then through the entrance app.json Of pages You know all the page paths of the current applet . Then it will load the code of the home page , Through the underlying mechanism of the applet , You can render this home page .
Four 、 Register applet
- Every applet needs to be in app.js Call in App Method to register an applet instance , Bind lifecycle callback function 、 Error listening, no listening function on the page, etc .
- There is only one... In the whole applet App example , It's all page sharing . adopt
getAppMethod to get this instance .
What do you usually do when registering an applet ?
- Determine the entry scenario of the applet , Scene value .
- Get user information .
- Set global data — globalData.
5、 ... and 、 Registration page
- For each page in the applet , All need to be mapped on the page js Register in the file , Specify the initial data for the page 、 Lifecycle Callback 、 Event handling functions, etc .
- Simple page usage Page Constructor to construct .
- Complex pages use Component Constructor to construct .
How to write a page of a small program ?
- I've already introduced , A page may include 4 Documents , Wechat client will first according to
page jsonConfiguration generates an interface , The top color and text you can see in thispage jsonIt's defined in the file . Then the client will load the pageWXMLThe structure andWXSSstyle . Finally, the client will loadJS. - then
PageThis constructor will generate a page . When generating a page , The applet framework will putdataData and.wxmlRender the final structure together .
What do you usually do when registering a page ?
- Send network request in lifecycle function , Get data from server .
- Initialize some data , To facilitate being wxml Quote show .
- monitor wxml In the event , Bind the corresponding event function .
- Some other monitors ( For example, page scrolling 、 Pull up to refresh 、 Pull down to load more, etc ).
6、 ... and 、 Page lifecycle
You don't need to figure it out immediately , But it will help in the future .
The following figure illustrates the page Page The life cycle of the instance .

边栏推荐
- SQL必需掌握的100个重要知识点:创建和操纵表
- [xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations
- 我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
- [STL source code analysis] container (to be supplemented)
- Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement
- 100 important knowledge points that SQL must master: using table aliases
- [机缘参悟-34]:光锥之内皆命运
- 蚂蚁金服笔试题:需求文档有什么可以量化的【杭州多测师】【杭州多测师_王sir】...
- Lvgl 8.2 picture scaling and rotation
- Deep dive kotlin Xie Cheng (17): Actor
猜你喜欢

焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败

Go language defer

The life, working principle and application of electrochemical oxygen sensor

【STL源码剖析】迭代器

Mathematics (fast power)

中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
![200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration](/img/0e/19c4c97a582d7b2ad08ce806d7af03.jpg)
200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration

Record the memory leak of viewpager + recyclerview once

启明星辰集团运维安全网关(堡垒机)再次夺得榜首!

Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
随机推荐
100 important knowledge points that SQL must master: using table aliases
SQL必需掌握的100个重要知识点:联结表
Lvgl 8.2 picture scaling and rotation
导致系统性能失败的10个原因
The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
SQL必需掌握的100个重要知识点:使用视图
Rejuvenated Dell and apple hit each other, and the two old PC enterprises declined rapidly
语音识别-基础(一):简介【语音转文本】
ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
以PolarDB为代表的阿里云数据库以跻身全球第一阵营
压缩状态DP位运算
Every time I look at my colleagues' interface documents, I get confused and have a lot of problems...
【leetcode 16】三数之和
datax - 艰难debug路
创建型-配置工厂
LVGL 8.2 Simple Colorwheel
OLAP数据库引擎如何选型?
Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court