小程式中的讚功能和收藏功能是必不可少的,那麼該如何實現點讚和收藏兩個功能呢?本篇文章將給大家分享小程式中的讚功能和收藏功能的程式碼實作。
收藏功能:
focusFavoriteTab: function (e) { var that = this; var isFocus = that.data.isFocus; console.log("isFocus", isFocus) var itemId = that.data.itemId; if (isFocus) { favoriteService.cancel(that, itemId).then((res) => { wx.showToast({ title: "取消收藏", icon: 'success', duration: 1000 }); this.setData({ isFocus: false, }); }) } else { favoriteService.add(that, itemId).then((res) => { wx.showToast({ title: res.data == 1 ? "收藏成功" : "收藏失败", icon: 'success', duration: 1000 }); this.setData({ isFocus: true, }); }); } },
按讚功能:
focuslikeTab: function (e) { var that = this; var isLike = that.data.isLike; let itemDetail = this.data.itemDetail var itemId = that.data.itemId; if (isLike) { itemService.cancellike(that, itemId).then((res) => { wx.showToast({ title: "点赞取消", icon: 'success', duration: 1000 }); itemDetail.liked--; this.setData({ itemDetail:itemDetail }); this.setData({ isLike: false, }); }) } else { itemService.addlike(that, itemId).then((res) => { wx.showToast({ title: res.data == 1 ? "点赞成功" : "点赞失败", icon: 'success', duration: 1000 }); itemDetail.liked++; this.setData({ itemDetail: itemDetail }); this.setData({ isLike: true, }); }); } },
相關推薦:
##################################以上是小程式中按讚和收藏功能的實作程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!