Page({ data: { }, onLoad: function (option) { wx.clearStorage(); var postId = option.id; this.data.currentPostId = postId; var postData = postsData.postList[postId]; this.setData({ postData: postData }); // var postCollected = { // 1:"true", // 2:"false", // 3:"true" // } 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); } }, onCollectionTap: function (event) { var postsCollected = wx.getStorageSync("post_collected"); var postCollected = postsCollected[this.data.currentPostId]; //收藏变成未收藏,未收藏变成收藏 postCollected = !postCollected; postsCollected[this.data.currentPostId] = postCollected; //更新文章是否收藏的缓存值 wx.setStorageSync("post_collected", postsCollected); //更新数据绑定变量,从而切换图片 this.setData({ collected: postCollected }) } })
点击收藏后报错
摸不着头脑
4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!
了解课程