搞不懂异步 小程序不能用ES7的async和await 失落= =
本地缓存的四个方法:
```
(1)wx.getStorage()
(2)wx.setStorage()
(3)wx.clearStorage()
(4)wx.removeStorage()
```
```
Page({
data:{},
onLoad(option) {
let postId = option.id;
this.data.currentPostId = postId;
let oData = postsData.postList.find(item => item.postId == postId);
let collected=false;
let that = this;
wx.getStorage({
key: 'posts_Collected',
success: function (res) {
for(let key in res.data){
if(key==postId){
collected = res.data[key];
}
};
res.data[postId] = collected;
wx.setStorage({
key: 'posts_Collected',
data: res.data
});
that.setData({
postData: oData,
collected
});
},
fail: function (res){
let postsCollected = {};
postsCollected[postId]=false;
wx.setStorage({
key: 'posts_Collected',
data: postsCollected
});
that.setData({
postData: oData,
collected
});
}
})
},
onCollectTap(event){
let that = this;
wx.getStorage({
key: 'posts_Collected',
success: function(res) {
let currentCollected = res.data[that.data.currentPostId];
currentCollected =! currentCollected;
res.data[that.data.currentPostId]= currentCollected;
wx.setStorage({
key: 'posts_Collected',
data: res.data
});
that.setData({
collected: currentCollected
});
},
})
}
})
```