Page({
data: {
items:{ //items为一个对象,is_like和like分别为其两个属性
is_like: 0,
like: 0
}
},
likes: function () {
var is_like ="items.is_like"; //先用一个变量,把items.is_like用字符串拼接起来
var like = "items.like";
if (this.data.items.is_like == 0 && this.data.items.like==0){
this.setData({
[is_like]:1, 使用【】将字符串包起来,为其赋值
[like]:1
})
} else if (this.data.items.is_like == 1 && this.data.items.like == 1){
this.setData({
[is_like]: 0,
[like]: 0
})
}
},
})
2.
index.wxml: 使用bindtap绑定事件
<view class="centers" bindtap="likes">
<view wx:if="{{items.is_like == 0}}">
<image class="src" src="../../image/like1.png"></image>
</view>
<view wx:else>
<image class="src" src="../../image/like2.png"></image>
</view>
<view class="space">赞<text class="character">:</text>{{items.like}}</view>
</view>