最近学习微信小程序,发现一个关于this的坑,记录一下:
代码:
// pages/post/post-detail/post_detail.js
var postsData = require("../../../data/posts-data.js");
Page({
/**
* 页面的初始数据
*/
data: {
postData: {},
collected: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
var id = options.id;
this.data.currentpostId = id;
var postData = postsData.postList[id];
// this.data.postData = postData;
this.setData({
postData: postData
})
var postsCollected = wx.getStorageSync("posts_Collected");
if (postsCollected) {
var postCollected = postsCollected[id];
this.setData({
collected: postCollected
})
} else {
var postsCollected = {};
postsCollected[id] = false;
wx.setStorageSync("posts_Collected", postsCollected);
}
},
onCollectionTap: function