# Pure data fields
Pure data fields are data fields that are not used for interface rendering and can be used to improve page update performance.Support starts with Weixin Mini Program base library version {% version ('2.8.2')%}.
# Pure data fields in component data
In some cases, certaindataFields in(including fields set bysetData) are neither displayed on the interface nor passed to other components, only used within the current component.
At this point, you can specify such data fields as "pure data fields," which will only be recorded inthis.datawithout participating in any interface rendering process, helping to improve page update performance.
The way to specify a "pure data field" is inComponentTheoptionsdefinition of the constructor specifiespureDataPatternas a regular expression, and fields whose names match this regular expression will become pure data fields.
Code example:
Component({
options: {
pureDataPattern: /^_/ // 指定所有 _ 开头的数据字段为纯数据字段
},
data: {
a: true, // 普通数据字段
_b: true, // 纯数据字段
},
methods: {
myMethod() {
this.data._b // 纯数据字段可以在 this.data 中获取
this.setData({
c: true, // 普通数据字段
_d: true, // 纯数据字段
})
}
}
})
The pure data fields in the above components are not applied to WXML:
<view wx:if="{{a}}"> 这行会被展示 </view>
<view wx:if="{{_b}}"> 这行不会被展示 </view>
# Pure data fields in component properties
Properties can also be specified as pure data fields (following thepureDataPatternof the Code Expression).
A pure data field in a property can receive externally passed property values just like a normal property, but cannot use it directly in the component's own WXML.
Code example:
Component({
options: {
pureDataPattern: /^_/
},
properties: {
a: Boolean,
_b: {
type: Boolean,
observer() {
// 不要这样做!这个 observer 永远不会被触发
}
},
}
})
Note: The attribute observer for a pure data field in the attribute is never triggered!If you want to listen for attribute value changes, use the data monitor instead.
Starting with Weixin Mini Program base library version {% version ('2.10.1')%}, you can also configure [[in a page or in a custom component's json filepureDataPattern(this eliminates the need to reconfigure it in theoptionsof the js file).In this case, the value should be written as a character string
{
"pureDataPattern": "^_"
}
# Use a data listener to listen to pure data fields
A data monitor can be used to listen to pure data fields (just like normal data fields).In this way, you can change the interface by listening and responding to changes in pure data fields.
The following example is a custom component that converts a JavaScript timestamp to a readable time.
Code example:
Component({
options: {
pureDataPattern: /^timestamp$/ // 将 timestamp 属性指定为纯数据字段
},
properties: {
timestamp: Number,
},
observers: {
timestamp: function () {
// timestamp 被设置时,将它展示为可读时间字符串
var timeString = new Date(this.data.timestamp).toLocaleString()
this.setData({
timeString: timeString
})
}
}
})
<view>{{timeString}}</view>