postsCollectd[postId] = false; 执行报错
来源:19-7 使用缓存实现文章收藏功能
_卡卡C
2016-12-28
6-6节,点击第二个detail,else里面,一执行到postsCollectd[postId] = false;就提示
Cannot create property '1' on string '' Page Pages/posts/post-detail/post-detail catch error in lifeCycleMethod onLoad function
TypeError: Cannot create property '1' on string ''
写回答
2回答
-
仔细看一下,每个变量,这个问题就是你的变量 不是对象,而是一个字符串。建议 对着代码一步步的来,或者下载源代码对比一下。
012016-12-28 -
_卡卡C
提问者
2016-12-28
var postsData = require('../../../data/posts-data.js') Page({ data: { }, onLoad: function (options) { var postId = options.id; this.data.currentPostId = postId; // this.data.postData = postData; this.setData({ postData: postsData.postList[postId] }) var postsCollectd = wx.getStorageSync('posts_collected'); if (postsCollectd) { var postCollected = postsCollectd[postId]; this.setData({collected : postCollected}) }else{ var postsCollected = {}; postsCollectd[postId] = false; wx.setStorageSync({key:'posts_collectd',data:postsCollectd}); } }, onCollectedTap : 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}); } })
012017-12-14
相似问题