微信小程序:
前言
:在微信小程序开发过程中,还会遇到很多知识运用:
一.本地存储:
在开发的过程中,常常会用到本地存储.数据存储生命周期与小程序生命周期一致
好处
:有了本地缓存,你的小程序可以做到:
- 离线应用(已测试在无网络的情况下,可以操作缓存数据)
- 流畅的用户体验
- 减少网络请求,节省服务器资源
(一).数据支持:
原生类型、Date、及能够通过JSON.stringify序列化的对象
(二).方法:
1.同步
(1)wx.setStorageSync()
; 存储值
wx.setStorageSync("collect", collect);
参数:
参数 | 作用 |
---|---|
key | 本地缓存中指定的 key |
success | 异步接口调用成功的回调函数,回调参数格式: {data: key对应的内容} |
fail | 异步接口调用失败的回调函数 |
complete | 异步接口调用结束的回调函数(调用成功、失败都会执行) |
data | 需要存储的内容 |
(2)wx.removeStorageSync()
; // 移除指定的值
wx.removeStorageSync("collect");
(3)wx.getStorageSync()
; 获取值
let collect=wx.getStorageSync("collect")
(4)wx.getStorageInfoSync()
; 获取当前 storage 中所有的 key
const res = wx.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
参数:
参数 | 作用 |
---|---|
key | 本地缓存中指定的 key |
success | 异步接口调用成功的回调函数,回调参数格式: {data: key对应的内容} |
fail | 异步接口调用失败的回调函数 |
complete | 异步接口调用结束的回调函数(调用成功、失败都会执行) |
(5)wx.clearStorageSync()
; 清除所有的key