-
wx.setStorage(Object object)
- 将数据存储在本地缓存中指定的 key 中,且会覆盖掉原来该 key 对应的内容。
- 除非用户主动删除或因存储空间原因被系统清理,否则数据都一直可用。
- 单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
- 属性:key,data(只支持原生类型、Date、及能够通过JSON.stringify序列化的对象。),success,fail,complete
- wx.setStorageSync(string key, any data) 是同步版本。
wx.setStorage({
key:"key",
data:"value"
})
try {
wx.setStorageSync('key', 'value')
} catch (e) { }
-
wx.removeStorage(Object object)
- 从本地缓存中移除指定 key。
- 属性:key,success,fail,complete
- wx.removeStorageSync(string key) 是同步版本。
wx.removeStorage({
key: 'key',
success (res) {
console.log(res)
}
})
try {
wx.removeStorageSync('key')
} catch (e) {
// Do something when catch error
}
-
wx.getStorage(Object object)
- 从本地缓存中异步获取指定 key 的内容
- 属性:key,success,fail,complete
- wx.getStorageSync(string key) 是同步版本。
wx.getStorage({
key: 'key',
success (res) {
console.log(res.data)
}
})
try {
var value = wx.getStorageSync('key')
if (value) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}
-
wx.clearStorage(Object object)
- 清理本地数据缓存
- 属性:success,fail,complete
- wx.clearStorageSync()是同步版本。
wx.clearStorage()
try {
wx.clearStorageSync()
} catch(e) {
// Do something when catch error
}
-
wx.getStorageInfo(Object object)
- 异步获取当前storage的相关信息
- 属性:success,fail,complete
- success(Object object)回调函数中包含参数
- keys, Array.<string>, 当前 storage 中所有的 key
- currentSize, number, 当前占用的空间大小, 单位 KB
- limitSize, number, 限制的空间大小,单位 KB
- Object wx.getStorageInfoSync()是同步版本。
wx.getStorageInfo({
success (res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})
try {
const res = wx.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
} catch (e) {
// Do something when catch error
}
-
wx.getBackgroundFetchData(object object)
- 拉取 backgroundFetch 客户端缓存数据
- 属性:fetchType(取值为 periodic), success,fail,complete
- wx.onBackgroundFetchData(Object object) 收到 backgroundFetch 数据时的回调。
-
wx.setBackgroundFetchToken(object object)
设置自定义登录态(token),在周期性拉取数据时带上,便于第三方服务器验证请求合法性。 -
wx.getBackgroundFetchToken(Object object)
获取设置过的自定义登录态。若无,则返回 fail。
网友评论