按照老师教的写的,,点击后没有出来想要的效果,而且报错=-=这是为啥子
来源:19-7 使用缓存实现文章收藏功能
啊可
2018-11-10
如图
1回答
-
啊可
提问者
2018-11-10
已解决,问题1:
新增这个键无值时的else判断。。
//获取缓存中的值
var postsCollected = wx.getStorageSync('posts_collected')
if (postsCollected) {
var postCollected = postsCollected[postId];
if (postCollected) {
this.setData({
collected: postCollected
})
}else{
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected);
}
} else {
var postsCollected = {};
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected);
}
解决问题2,大意。。给当前键赋值时使用了postsCollected而不是postCollected
onColletionTap: function(event) {
var postsCollected = wx.getStorageSync('posts_collected')
var postCollected = postsCollected[this.data.currentPostid];
console.log(this.data.currentPostid)
//收藏变成未收藏,未收藏变成收藏
postCollected = !postCollected;
console.log(postCollected)
//给当前的键赋值
postsCollected[this.data.currentPostid] = postCollected;
console.log(postsCollected)
//更新了文章是否收藏的缓存值
wx.setStorageSync('posts_collected', postsCollected)
//更新数据绑定变量,从而切换图片
this.setData({
collected: postCollected
})
}
00
相似问题