核心要点:将需要修改的全局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
})
}
如果你感觉有收获,欢迎给我打赏 ———— 以激励我输出更多优质内容
本文介绍了一种在WXML中动态更新全局data的方法,通过将数据定义为字符串并在setData中使用中括号引用,实现了按钮点击时局部数据的高效更新。
2736

被折叠的 条评论
为什么被折叠?



