请稍等 ...
×

采纳答案成功!

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

Setting data field "collected" to undefined is invalid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var postsData = require('../../../data/posts-data.js')
Page({
data: {
},
 
onLoad: function(option) {
var postId = option.id;
var postData = postsData.postList[postId]; 
this.setData({
postData: postData,
postId: postId
})
var postsCollected = wx.getStorageSync('posts_collected')
if (postsCollected) {
var postCollected = postsCollected[postId]
this.setData({
collected: postCollected
})
else 
var postsCollected = {}; 
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected);
}
},
 
onColletionTap: function(event) {
var postsCollected = wx.getStorageSync('posts_collected');
var postCollected = postsCollected[this.setData.postId];
postCollected = !postCollected;
postsCollected[this.setData.postId] = postCollected; 
wx.setStorageSync('posts_collected', postsCollected); 
this.setData({
collected: postCollected
})
},
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<view class='container'>
<image class='head-image' src='{{postData.img.headImgSrc}}'></image>
<image class='audio' src='../../../images/music-start.png'></image>
<view class='author-date'>
<image class='avatar' src='{{postData.img.avatarDetail}}'></image>
<text class='author'>{{postData.author}}</text>
<text class='const-text'>发表于</text>
<text class='date'>{{postData.dateTime}}</text>
</view>
<text class='title'>{{postData.title}}</text>
<view class='tool'>
<view class='circle-img'>
<image wx:if='{{collected}}' catchtap='onColletionTap' src='../../../images/shoucang.png'></image>
<image wx:else catchtap='onColletionTap' src='../../../images/shoucang-anti.png'></image>
<image catchtap='onShareTap' class='share-img' src='../../../images/fenxiang.png'></image>
</view>
<view class='horizon'></view>
</view>
<text class='detail'>{{postData.detail}}</text>
</view>

https://img1.sycdn.imooc.com//szimg/5b4eb482000171e910170308.jpg

在点击某篇文章进入文章详情时就弹出了错误,“Setting data field "collected" to undefined is invalid.”

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

插入代码

2回答

慕运维5448999 2018-09-16 13:11:07

同样问题,求解答

0 回复 有任何疑惑可以回复我~
7七月 2018-07-19 10:11:09

错误给的很明显,你需要看看为什么出现undefined。

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号