WXML文件是这样的
<view class="XXX" wx:for="{{YY}}">
<view class="xx1">{{item.Y1}}</view>
<view class="xx2">{{item.Y2}}</view>
<view class="xx3">{{item.Y3}}</view>
<icon class="xx4" wx:if="{{item.Y4}}" type="success" size="23"></icon>
<icon class="xx4" wx:else type="warn" size="23"></icon>
</view>
JS文件里这样写的
data: {
name_Items:['XM1','XM2','XM3','XM4','XM5','XM6','XBR1','XBR2','XBR3','XBR4','XBR5','XBR6'],
YY:[
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false},
{Y1:'', Y2:0, Y3:0, Y4:false}
],
},
赋值语句是这么写的(注释中的语句是可以赋值成功的,但是放到for列表循环中就会报错)
onLoad: function (options) {
// this.setData({
// 'YY[0].Y1':this.data.name_Items[0],
// 'YY[1].Y1':this.data.name_Items[1],
// 'YY[2].Y1':this.data.name_Items[2],
// 'YY[3].Y1':this.data.name_Items[3],
// 'YY[4].Y1':this.data.name_Items[4],
// 'YY[5].Y1':this.data.name_Items[5],
// })
var i = 0
var length = this.data.name_Items.length
for (let i = 0; i < length; i++) {
console.log(i)
this.setData({
['YY[${i}].Y1']:this.data.name_Items[i],
})
}
},
报错Error: Only digits (0-9) can be put inside [] in the path string: YY[${i}].Y1
this.setData({ [`YY[${i}].Y1`]:this.data.name_Items[i], })