采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
点击收藏图标,不能即使切换状态,一定要点击导航返回后,然后再次进入当前页面,才可以看到收藏状态变化,请问是什么原因呢?
代码看不出问题呢,同时收藏触发打印的时候,状态都是即时变成true或者false的。
var postsData = require('../../../data/posts-data.js')
Page({
data:{
},
onLoad: function (option) {
var postId = option.id;
this.data.currentPostId = postId;
var postData = postsData.postList[postId];
this.data.postData = postData;
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)
}
},
onTapCollection: 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
})
// console.log(this.setData.collected)
}
})
登录后可查看更多问答,登录/注册
4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!
了解课程