欢迎光临
我们一直在努力

微信小程序实现收藏功能

有时我们需要微信小程序的收藏功能,具体实现如下:

/* 收藏 */
var postsCollected = wx.getStorageSync('posts_Collected')
if (postsCollected) {
    var postCollected = postsCollected[postId]
    this.setData({
        collected: postCollected
    })
} else {
    var postsCollected = {}
    postsCollected[postId] = false;
    wx.setStorageSync('posts_Collected', postsCollected);
}
onColletionTap:function (event) {
    var postsCollected = wx.getStorageSync('posts_Collected');
    var postCollected = postsCollected[this.data.currentPostId];
//收藏变成未收藏,未收藏变成收藏
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
//更新文章是否的缓存值
    wx.setStorageSync('posts_Collected', postsCollected);
//更新数据绑定变量,从而实现切换图片
    this.setData({
        collected: postCollected
    })
}

 

赞(2)
版权归原作者所有,如有侵权请告知。达维营-前端网 » 微信小程序实现收藏功能

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址