Setting data field "collected" to undefined is invalid.
来源:19-7 使用缓存实现文章收藏功能
慕函数1327521
2020-06-17
},
onLoad:function(option){
//接收posts.js 传递过来的postId
//posts.js中 url: “post-detail/post-detail?id=” + postId
var postId = option.id;
this.data.currentPostId = postId;
var postData = postsData.postList[postId];
//数据绑定
// this.data.postData = postData;
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.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
})
}
})
1回答
-
7七月
2020-06-17
调试一下。。。,这个只是纯粹的代码写错了。无论是再看课程,或者调试,或者对比源代码,总能找到哪里写错了
00
相似问题