当前位置:网站首页>Wechat applet_ 16. Component life cycle
Wechat applet_ 16. Component life cycle
2022-07-18 23:37:00 【icestone_ kai】
All lifecycle functions of components :
The full life cycle of the applet is shown in the following table :
| Life cycle function | Parameters | Description |
|---|---|---|
| created | nothing | Execute when the component instance has just been created |
| attached | nothing | Execute when the component instance enters the page node tree |
| ready | nothing | Execute after components are laid out in view layer |
| mooved | nothing | Execute when the component instance is moved to another location in the node tree |
| detached | nothing | Execute when the component instance is removed from the page node tree |
| error | ObjectError | Execute whenever the component throws an error |
The main life cycle functions of components :
In the applet , The most important life cycle functions are 3 individual , Namely created,attached,detached. Their respective characteristics are as follows :
1. When the component instance is just created ,created The lifecycle function will be triggered
- You can't call setData
- Usually in this life cycle function , Should only be used for components this Add some custom attribute fields
2. After the component is fully initialized , Just after entering the page node tree ,attached The lifecycle function will be triggered
- here ,this.data Has been initialized
- This life cycle is very useful , Most initialization can be done at this time ( For example, send a request to get the initial data )
3. After the component leaves the page node tree ,detached The lifecycle function will be triggered
- When exiting a page , It will trigger the of each custom component in the page detached Life cycle function
- At this time, it is suitable to do some cleaning work
Define the lifecycle function of the component :
lifetimes node :
In the applet , Life cycle functions can be defined directly in Component In the first level parameters of the constructor , Can be in lifetimes Declaration in field ( This is the recommended way , It has the highest priority ), The sample code is as follows :
Component({
/** * A list of properties of a component */
properties: {
},
// Recommended use :
lifetimes: {
// Execute when the component instance enters the page node tree
attached() {
},
// Execute when the component instance is removed from the page section tree
detached() {
}
},
// Here's the old definition :
// Execute when the component instance enters the page node tree
attached() {
},
// Execute when the component instance is removed from the page section tree
detached() {
},
})
When both old and new methods exist , Will give priority to new , It is not recommended to use the old way in the future
What is the lifecycle function of the page where the component is located :
Sometimes , The behavior of custom components depends on the change of page state , At this point, you need to use the life cycle of the page where the component is located
for example : Whenever the page is triggered show When it comes to life cycle functions , We hope to regenerate a random RGB Color value :
In custom components , The life cycle functions of the page where the component is located are as follows 3 individual , Namely :
| Life cycle function | Parameters | describe |
|---|---|---|
| show | nothing | Execute when the page where the component is displayed |
| hide | nothing | Execute when the page where the component is located is hidden |
| resize | Object Size | When the page size of the component changes |
pageLifetimes
The lifecycle function of the page where the component is located , Need to be defined in pageLifetimes In nodes , The sample code is as follows :
// components/lifetimes/lifetimes.js
Component({
/** * A list of properties of a component */
properties: {
},
pageLifetimes: {
show: function () {
console.log(' The page is displayed ');
},
hide: function () {
console.log(' The page is hidden ');
},
resize: function () {
console.log(' The page has changed ');
},
},
})
Generate random RGB Color value :
Component({
pageLifetimes: {
show: function () {
console.log(' The page is displayed , Generate random colors ');
this._randomColor()
},
hide: function () {
console.log(' The page is hidden ');
},
resize: function () {
console.log(' The page has changed ');
},
},
// Generate random RGB Color value :
_randomColor() {
this.setData({
_rgb: {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
}
})
}
})
function :
Here is the status of the component listening to the page , Every time you enter the page , Generate a random RGb value ,
边栏推荐
- 7、常见的垃圾回收器
- Quickly build an e-commerce platform based on Amazon cloud technology server free service - Deployment
- pytest接口自动化测试框架 | 对requests进行二次封装
- Developers must see | devweekly issue 1: what is time complexity?
- 过拟合 欠拟合
- Under dynamic memory management (C language) -- common errors and written examination questions of large factories
- 嘘!摸鱼神器,别让老板知道!| 语音实时转文本,时序快速出预测,YOLOv6在就能用,一行命令整理CSV | ShowMeAI资讯日报
- STM32F1与STM32CubeIDE编程实例-W25Q-SPI-Flash与FatFs移植
- ROS create workspace process
- 7月献礼,买云盘就送特级桂七,仅限2个月,欲购从速
猜你喜欢

20220715 domestic CONDA does not FQ the method of installing the latest version of pytoch

7、常见的垃圾回收器

万字详解C语言文件
Under dynamic memory management (C language) -- common errors and written examination questions of large factories
![[development tutorial 1] open source Bluetooth heart rate waterproof sports Bracelet - Kit detection tutorial](/img/45/0e4b8a2049645d45a485804280b0c4.png)
[development tutorial 1] open source Bluetooth heart rate waterproof sports Bracelet - Kit detection tutorial

Basic knowledge of common amplifiers (I)

Deep analysis of fiboracci sequence

Compose 使用Coil加载网络图片

ADB common entry instructions

11(2). The storage mode of structure, the problem of transferring structure variables and structure variable pointers as function parameters, and the advantages of pointers
随机推荐
剑指 Offer II 119. 最长连续序列
Solve the problem that WinDbg cannot load ntdll symbols
ROS create workspace process
ImportError: cannot import name ‘Imputer‘ from ‘sklearn. preprocessing‘
pytest接口自动化测试框架 | pytest简介
pytest接口自动化测试框架 | 接口关联
8、JVM优化简介
What are the efficient test methods for app regression testing?
Deep analysis of fiboracci sequence
There is no simpler and more practical single person pose estimation than this
基于STM32的DHT11温湿度传感器设计
Summer study matlab notes
Interviewer: tell me about the most valuable bug you found in your work
Binary tree experience
Developers must see | devweekly issue 1: what is time complexity?
How does go ensure the order of concurrent reads and writes Memory model
微信小程序_17,插槽
微信小程序_15,纯数据字段
Quickly build an e-commerce platform based on Amazon cloud technology server free service - Deployment
[C language] explanation and Simulation Implementation of strlen function