点赞效果:
wxml:
绑定数组元素下标(index)和id,根据接口数据返回是否已点赞状态(like)。
<view class="fabulousBox" bindtap="handleFabulous" data-id="{{item.id}}" data-index="{{index}}">
<image wx:if="{{item.like}}" class="fabulousImg" src="../../assets/images/fabulous-active.png"></image>
<image wx:else class="fabulousImg" src="../../assets/images/fabulous.png"></image>
<text class="fabulousNum">{{item.num}}</text>
</view>
js:
commentData:返回的数组数据, like:是否点赞状态,num:点赞数量。
handleFabulous(event){
var idx = event.currentTarget.dataset.index;
var _this1 = this;
this.$request({
url: '请求的接口',
type: 'GET',
param: {
id: event.currentTarget.dataset.id,
}
}).then(function (res) {
_this1.__data__.commentData[idx].like = !_this1.__data__.commentData[idx].like;
_this1.__data__.commentData[idx].num = !_this1.__data__.commentData[idx].like ? _this1.__data__.commentData[idx].num-1 : _this1.__data__.commentData[idx].num+1;
_this1.setData({
commentData: _this1.__data__.commentData
});
})["catch"](function (error) {
console.log(error, 'error');
})
},