当前位置:网站首页>自定义组件--纯数据字段&组件的生命周期
自定义组件--纯数据字段&组件的生命周期
2022-07-28 05:25:00 【像费曼%】
1.1 概念:纯数据字段指的是那些不用于界面渲染的data字段
应用场景:在有些情况下,某些data中的字段既不会展示在界面上,也不会传递给其他组件,仅仅在当前组件内部使用,带有这种特性的data字段适合被设置为纯数据字段
好处:纯数据字段有助于提升页面更新的性能(不懂怎样提升页面的更新性能)
1.2 使用规则
在构造器的options节点中,指定pureDataPattern为一个正则表达式,字段名符合这个正则表达的字段成为纯数据字段
Component({
options:{
//指定所有_开头的数据字段为纯数据字段
pureDataPattern:/^_/
},
data:{
a:true//普通数据字段
_b:true//纯数据字段
}
})2.1组件全部可用的生命周期函数
| 生命周期函数 | 参数 | 描述说明 |
| created | 无 | 在组件实例刚刚被创建时执行 |
| attached | 无 | 在组件实例进入页面节点树时执行(还未渲染) |
| ready | 无 | 在组件在视图层布局完成后执行 |
| moved | 无 | 在组件实例被移动到节点树另一个位置时执行 |
| detached | 无 | 在组件实例被从页面节点树移除时执行 |
| error | Object Error | 每当组件方法抛出错误时执行 |

2.2lifetimes节点
在小程序组件中,生命周期函数可以直接定义在Component构造器的第一级参数中,可以在lifetimes字段声明(推荐,其优先级最高)

2.3组件所在页面的生命周期
有时,自定义组件的行为依赖于页面状态的变化,此时就需要用到组件所在页面的生命周期
| 生命周期函数 | 参数 | 描述 |
| show | 无 | 组件所在的页面被展示时执行 |
| hide | 无 | 组件所在的页面被隐藏时执行 |
| resize | Object Size | 组件所在的页面尺寸变化时执行 |
2.4pageLifetimes节点
组件所在页面的生命周期,需要定义在pageLifetimes节点中
Component({
pageLifetimes:{
show:(){}
hide:(){}
resize:(){}
}
})2.5案例
展示页面时,随机生成RGB颜色值

//组件js
methods:{
_randomColor(){
this.setData({
_rgb:{
r:Math.floor(Math.random()*256),//Math.floor()向下取整,即像马太效应里的小的让它更小
g:Math.floor(Math.random()*256),
b:Math.floor(Math.random()*256)
}
})
}
}
pageLifetimes:{
show:function(){
this._randomColor()
}
}Math.floor()函数可参考:http://t.csdn.cn/kole7
边栏推荐
- Distinguishing PCB quality by color is a joke in itself
- Bag of tricks training convolution network skills
- Learning notes of hardware circuit design 1 -- temperature rise design
- Systemmediasize startup option added in esxi 7.0 update 1C
- qt批量操作控件,并设置信号槽
- Talking about fluke optical cable certification? What is CFP? What is OFP?
- Exploration of Clickhouse aggregation internal mechanism of aggregation
- Matlab simulation of radar imaging 2 - pulse compression and windowing
- Vscode中,无法打开源文件 “Adafruit_GFX.h“
- Pytorch learning notes
猜你喜欢
![[yolov5] environment construction: win11 + mx450](/img/04/4da9b3748d5f45ef73cffcc48f8609.png)
[yolov5] environment construction: win11 + mx450

clickhouse建宽表多少列最合适?

Matlab simulation of radar imaging 4 - range resolution analysis

mixup_ratio

IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha

多个ics日历合并成单个ics日历

EMC experiment practical case ESD electrostatic experiment

Efficient Net_V2

OpenGL的开发环境配置【VS2017】+常见问题

MFC 使用控制台打印程序信息
随机推荐
QT parse string into JSON data and parse
根据IP地址和子网掩码求主机所在的网络地址和广播地址
Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
set_ case_ analysis
使用wampserver3.2.6时切换中文时造成启动失败
Common table expression CTE in Clickhouse
clickhouse建宽表多少列最合适?
How can fluke dsx2-5000 and dsx2-8000 modules find the calibration expiration date?
set_multicycle_path
Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
How to use the bit error meter?
qt绘画事件-设置背景图片
PT 基于Multi Voltage的Physical Aware
Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted
Transformer self attention mechanism and complete code implementation
mixup_ ratio
转义字符笔记
RS232 RS485 RS422 communication learning and notes
ICC2使用report_placement检查floorplan
mixup_ratio