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 > |
在点击某篇文章进入文章详情时就弹出了错误,“Setting data field "collected" to undefined is invalid.”
4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!
了解课程