清空缓存,收藏报错
来源:8-12 同步文章缓存状态
 
			黑熊小心
2021-06-29
缓存被清除后,收藏按钮就失效了。if也使用了
let collected=ydCollected[this.data._yid]
if(collected === undefined){
collected = false
}
1回答
- 
				  qq_云水边静沐暖阳_04205942 2021-07-27 这是因为清空缓存后,缓存里并不存在posts_collected。所以在onLoad里面首次获取posts_collected时,其实返回的是一个空的字符,并不是一个对象。所以我们要在onLoad里面首次获取posts_collected时判断一下,如果不存在那就设置posts_collected初始值为{},这样就不会报错了。 onLoad: function (options) { const postData = postList[options.pid] this.data._pid = options.pid const postsCollected = wx.getStorageSync('posts_collected') || {} this.data._postsCollected = postsCollected let collected = postsCollected[this.data._pid] if (collected === undefined) { //如果是undefined,则说明文章从来没有被收藏过 collected = false } this.setData({ postData, collected }) } 1422022-08-07
相似问题
