核心要点:将需要修改的全局data用字符串的形式定义出来,然后在setData中用中括号包起来
js:
/**
* 页面的初始数据
*/
data: {
wage_data: [{
created_at: "2020-05-15 16:36:01",
has_like: true,
id: 2,
like_num: 1,
post_class_name: "自我提升",
profile: "概要",
title: "主题",
user_name: "test",
},
{
created_at: "2020-05-15 16:36:01",
has_like: true,
id: 3,
like_num: 1,
post_class_name: "自我提升",
profile: "概要",
title: "主题",
user_name: "test",
},{
created_at: "2020-05-15 16:36:01",
has_like: true,
id: 4,
like_num: 1,
post_class_name: "自我提升",
profile: "概要",
title: "主题",
user_name: "test",
}
],
},
wxml:
<block wx:for="{{wage_data}}">
<button bindtap="changData" data-index="{{index}}">
</button>
</block>
js:
changData: function(e) {
var index = e.target.dataset.index;
var value = 'wage_data[' + index + '].has_like'+ '';
this.setData({
[value]: !this.data.wage_data[index].has_like
})
}
如果你感觉有收获,欢迎给我打赏 ———— 以激励我输出更多优质内容