怎样动态地改变posts-data.js里面的值呢?我想实现记录页面浏览的数量。
来源:20-3 三种主要的路由API
normcore
2017-08-01
怎样动态地改变posts-data.js里面的值呢?我想实现记录页面浏览的数量。事实是能够获取里面的值,但是却不能改变这个值。
onPostTap:function(event){
var postId=event.currentTarget.dataset.postid; //获取点击的ID号
wx.navigateTo({
url:"post-detail/post-detail?id="+postId,
});
postsData.postlists[postId].reading = postsData.postlists[postId].reading+1;//将reading的值+1
console.log(postsData.postlists[postId].reading); //打印的值也是对的,但是posts-data.js里面reading的值还是没有改变
},
也使用了设置缓存的方法,不过也没啥用,请老师指点一下,谢谢。
写回答
2回答
-
女神旭
2017-09-24
post.js中: onShow: function (option) { this.getStorageData(); }, //加载缓存数据 getStorageData: function () { var getPostsView = wx.getStorageSync('posts_view');//获取本地缓存 var length = PostsData.postList.length; for (var i = 0; i < length; i++) { //循环加载 var getPostView = getPostsView[i]; var postdata = PostsData.postList[i].viewed; if (getPostView == null) { getPostView = 0; } PostsData.postList[i].viewed = getPostView; } //数据绑定 this.setData({ postList: PostsData.postList, }) } }) post-detail.js: onlaod函数中 // 浏览缓存 var postsView = wx.getStorageSync("posts_view"); if (postsView) { var postView = postsView[postId]; if(postView==null){ postView = 0; } postsView[this.data.CurrentPostId] = postView; wx.setStorageSync('posts_view', postsView); // this.data.Postdata.viewed = postView; this.setData({ viewed: postView, // Postdata: this.data.Postdata }) } else { var postsView = {}; postsView[postId] = 0; wx.setStorageSync('posts_view', postsView); }; onViewNumber: function (event) { var postsView = wx.getStorageSync('posts_view'); var postView = postsView[this.data.CurrentPostId]; postView++; postsView[this.data.CurrentPostId] = postView; wx.setStorageSync('posts_view', postsView); // that.data.Postdata.viewed = postView; this.setData({ viewed: postView, // Postdata: that.data.Postdata }); }
00 -
7七月
2017-08-02
动态改变,不会记录在文件里,而是会用服务器的数据库来记录。
012017-08-02
相似问题