当前位置:网站首页>微信小程序_15,纯数据字段
微信小程序_15,纯数据字段
2022-07-16 21:33:00 【icestone_kai】
什么是纯数据字段:
概念:纯数据字段指的是那些不用于界面渲染的data字段
应用场景:;例如某些情况下,某些data中的字段既不会展示在界面上,也不会传递给其他组件,仅在当前组件内部使用,带有这种特性的data字段适合被设置为纯数据字段
好处:纯数据字段有助于提升页面更新的性能
使用规则:
在Component构造器的options节点中,指定pureDataPatten为一个正则表达式,字段名符合这个正则表达式的字段将成为纯数据字段,示例代码如下:
// components/numsAdd/numsAdd.js
Component({
options: {
// 指定所有以_开头的数据字段为纯数据字段
pureDataPattern: /^_/
},
/** * 组件的初始数据 */
data: {
a:true,
_b:true,
}
}
使用纯数据字段改造数据监听器案例:
Component({
options: {
// 指定所有以_开头的数据字段为纯数据字段
pureDataPattern: /^_/
},
data: {
_rgb: {
r: 0,
g: 0,
b: 0,
},
fullColor: '0,0,0' //根据rgb对象的三个属性,动态计算fullColor的值
},
})
案例完整代码:
WXML:
<nums></nums>
组件的WXML:
<view class="randomColor" style="background-color: rgb({
{
fullColor}});"></view>
<text>
{
{rgb.r}},{
{rgb.g}},{
{rgb.b}}
当前RGB值:{
{fullColor}}
</text>
<button size="mini" type="primary" bindtap="rAdd">R+1</button>
<button size="mini" type="primary" bindtap="gAdd">G+1</button>
<button size="mini" type="primary" bindtap="bAdd">B+1</button>
组件的css:
.randomColor {
display: flex;
justify-content: center;
width: 100%;
height: 200rpx;
}
组件的.js:
// components/numsAdd/numsAdd.js
Component({
/** * 组件的属性列表 */
properties: {
},
options: {
// 指定所有以_开头的数据字段为纯数据字段
pureDataPattern: /^_/
},
/** * 组件的初始数据 */
data: {
_rgb: {
r: 0,
g: 0,
b: 0,
},
fullColor: '0,0,0' //根据rgb对象的三个属性,动态计算fullColor的值
},
/** * 组件的方法列表 */
methods: {
rAdd() {
this.setData({
'_rgb.r': this.data._rgb.r + 5 > 255 ? 255 : this.data._rgb.r + 5
})
},
gAdd() {
this.setData({
'_rgb.g': this.data._rgb.g + 5 > 255 ? 255 : this.data._rgb.g + 5
})
},
bAdd() {
this.setData({
'_rgb.b': this.data._rgb.b + 5 > 255 ? 255 : this.data._rgb.b + 5
})
},
},
// 数据监听节点
observers: {
'_rgb.**': function (obj) {
this.setData({
fullColor: `${
obj.r},${
obj.g},${
obj.b}`
})
}
// '对象.属性A,对象.属性B':function(属性A的新值,属性B的新值){}
// 对象触发此监听器的三种情况:
// [为属性A赋值]使用setData设置this.data.对象.属性A 时触发
// [为属性B赋值]使用setData设置this.data.对象.属性B 时触发
// [直接为对象赋值]使用setData设置this.data.对象 时触发
},
})
运行,圈起来的才是这部分的代码:
边栏推荐
- [white box test] design method of logic coverage and path test
- 微信小程序--》小程序简介与工具安装配置
- 开户需要注意的是什么?请问手机开户股票开户安全吗?
- pytest接口自动化测试框架 | 对requests进行二次封装
- 单元测试界的高富帅,Pytest框架 (完) 测试报告篇
- Cloud native: docker's practical experience (IV) deploying redis three master and three slave clusters on docker
- 「接口自动化」软件测试涨薪核心技能、让薪资涨幅200%
- Google icons 库 Compose就可以直接用。
- [remote desktop] rustdesk open source remote desktop, a substitute for TeamViewer and sunflower
- Data-addressing Mode
猜你喜欢

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

JVM-SANDBOX导致目标服务JVM Metaspace OOM的调查始末

Mysql存储模型

广州铁骑霸气“公主抱”,安全感拉满!
![[technology fragment] rename suffix of duplicate files based on exponential diffusion binary search](/img/1c/19fe2d9df5e075b903058aa0b9b521.png)
[technology fragment] rename suffix of duplicate files based on exponential diffusion binary search

作为一款时序数据库,TDengine 是如何实现并开源其分布式集群功能?

OSPF comprehensive experiment

Picasso, an efficient search generalization sparse training solution

本周总结2

解决windbg无法加载ntdll符号的问题
随机推荐
嘘!摸鱼神器,别让老板知道!| 语音实时转文本,时序快速出预测,YOLOv6在就能用,一行命令整理CSV | ShowMeAI资讯日报
ImportError: cannot import name ‘Imputer‘ from ‘sklearn.preprocessing‘
Cloud native: docker's practical experience (IV) deploying redis three master and three slave clusters on docker
Dialogue with machines, Ali Dharma academy challenges the new generation of man-machine dialogue Technology
没有比这更简单实用的单人姿态估计了
常用放大器基础知识(上)
Send papers! AI, machine learning, CV boss scientific research project enrollment!
pytest接口自动化测试框架 | conftest.py和@pytest.fixture()结合
How does go ensure the order of concurrent reads and writes Memory model
7月献礼,买云盘就送特级桂七,仅限2个月,欲购从速
VI editor sets custom shortcut keys to automatically generate the main function of C language
【Leetcode】232. Implement queue with stack
ACM board
性能测试中问题反思和心得
Developers must see | devweekly issue 1: what is time complexity?
Learning record: FSMC - extended external SRAM
Multithreaded application
Yiwen teaches you how to design test cases
Raspberry pie uses opencv for image tracking, face recognition, etc
Mysql事务隔离机制