请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Setting data field "collected" to undefined is invalid.

collection.pngcollection-anti.png var postsData = require('../../../data/posts-data.js') Page({ //借助data给下面的onCollectionTap 传递id data:{
  },

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
    })
}

})

正在回答 回答被采纳积分+3

1回答

7七月 2020-06-17 11:57:17

调试一下。。。,这个只是纯粹的代码写错了。无论是再看课程,或者调试,或者对比源代码,总能找到哪里写错了

登录后可查看完整回答登录/注册
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信