已收藏的图标再次点击无法变成未收藏

来源:19-7 使用缓存实现文章收藏功能

Rhythm7720

2019-07-14

检查很多遍了。 目前是点击收藏以后可以收藏,但是点击下一篇文章收藏以后,前一篇文章的收藏就被替换掉了。(缓存只存了最新的那一次点击的状态)另外已收藏的图标再次点击无法变成未收藏。不知道怎么回事!谢谢!

 onLoad: function (option) {

    var postId = option.id;
    
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      postData: postData});
    
    var postsCollected =wx.getStorageSync("posts_Collected")
    if (postsCollected){
      var postCollected=postsCollected[postId]
      if (postCollected){
        this.setData({
          collected: postCollected
        })
      }
    }
    else{
      var postsCollected={};
      postsCollected[postId]=false;
      wx.setStorageSync('posts_Collected', postsCollected);

    }
  },

  onCollectionTap: function(event){
    var postsCollected = wx.getStorageInfoSync("posts_Collected");
    var postCollected = postsCollected[this.data.currentPostId];
    postCollected=!postCollected;
    // 更新缓存
    postsCollected[this.data.currentPostId] = postCollected;
    // 更新缓存
    wx.setStorageSync("posts_Collected", postsCollected);
    // 更新数据绑定
    this.setData({
      collected: postCollected
    })

  }

写回答

1回答

7七月

2019-07-16

这个调试排查一下,看看storage里的值对吗?

0
1
Rhythm7720
谢谢老师!目前storage里面每次都被替换了 比如点第一篇文章收藏,再点第二篇文章收藏 storage里面就只有第二篇文章的收藏。第一篇就被顶替掉了。
2019-07-16
共1条回复

微信小程序入门与实战(全新版) 超20000人学习的好课

4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!

23956 学习 · 6928 问题

查看课程