微信小程序——数据缓存

一、设置缓存(wx.setStorageSync)

 wx.setStorageSync('Key',{

   game:"风暴英雄",

   developer:"暴雪"

 })

 wx.setStorageSync('Key1', {

   game: "和平精英",

   developer: "光子实验室"

 })

二、清除缓存

 wx.removeStorageSync('Key');

// 清除缓存

 wx.clearStorageSync('Key');

//清除所有缓存

// 缓存的上限最大不超过10mb

 

e.g:文章详情页收藏和取消(缓存功能)

 

var postsData = require('../../../data/posts_data.js');

Page({

onLoad:function(option){

var postId = option.id;

this.data.currentPostId = postId;

var postData = postsData.postList[postId];

this.setData({

postData:postData

});

//读取文章缓存状态

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);

}

},

onCollectionTap:function(event){

var postsCollected = wx.getStorageSync('posts_collected');

var postCollected = postsCollected[this.data.currentPostId];

console.log(postCollected);

//收藏变成为收藏,为收藏变成收藏

postCollected = !postCollected;

postsCollected[this.data.currentPostId] = postCollected;

// 更新文章是否收藏的缓存值

wx.setStorageSync('posts_collected', postsCollected);

// 更新数据绑定变量,从而实现切换图片

this.setData({

collected:postCollected

})

}

})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值