The biggest contributor to dynamically changing front-end data in small programs is this.setData(). Here is a brief overview of the situations in which values are assigned through this.setData:
1. Fixed attribute assignment
wxml:
<view>{
{item.name}}</view>
<view>{
{item.age}}</view>
js
data:{
item:{
name: "张三",
age:24
}
}
At this time, what is displayed on the interface is Zhang San 24
There are two options when we change the name
//第一种,整体赋值
this.data.item.name="李四";
this.setData({
item:item
})
//第二种,单个属性赋值
this.setData({
'item.name':'李四'
})
2. Dynamic attribute assignment
For example, if I want to assign a0 a value of 0, a1 a value 1, and a2 a2 a value 2, but I don’t want to write it three times, I can use a loop.
for (let i = 0; i < 3; i++) {
let name= `a${i}`;
this.setData({
[name]:i
})
}