目录
1 wx.setStorage(Object object)
2 wx.setStorageSync(string key, any data)
3 wx.getStorage(Object object)
4 wx.getStorageSync(string key)
5 wx.getStorageInfo(Object object)
7 wx.removeStorage(Object object)
8 wx.removeStorageSync(string key)
1 wx.setStorage(Object object)
异步将数据存储在本地缓存中指定的 key 中,如果key相同则会覆盖掉原来该 key 对应的内容。数据存储生命周期跟小程序本身一致,即除用户主动删除或超过一定时间被自动清理。
注:单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
key | string | 是 | 本地缓存中指定的 key | |
data | any | 是 | 需要存储的内容。只支持原生类型、Date、及能够通过JSON.stringify 序列化的对象。 | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
使用案例
wx.setStorage({
key: 'name',
data: 'Trevor'
})
2 wx.setStorageSync(string key, any data)
与wx.setStorage作用相同,只不过wx.setStorageSync是同步的
string key
本地缓存中指定的 key
any data
需要存储的内容。只支持原生类型、Date、及能够通过JSON.stringify
序列化的对象。
使用案例
wx.setStorageSync('name', 'Smith')
3 wx.getStorage(Object object)
从本地缓存中异步获取指定 key 的内容
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
key | string | 是 | 本地缓存中指定的 key | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.success 回调函数
Object res
属性 | 类型 | 说明 |
---|---|---|
data | any | key对应的内容 |
使用案例
wx.getStorage({
key: 'name',
success(res) {
console.log(res.data)
}
})
4 wx.getStorageSync(string key)
与wx.getStorage作用相同,不过此函数为同步获取
string key
本地缓存中指定的 key
返回值
any data
key对应的内容
使用案例
try {
const value = wx.getStorageSync('name2')
if (value) {
console.log(value)
}
} catch (e) {
// 不能正常获取时的逻辑
}
5 wx.getStorageInfo(Object object)
异步获取当前storage的相关信息
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.success 回调函数
Object object
属性 | 类型 | 说明 |
---|---|---|
keys | Array.<string> | 当前 storage 中所有的 key |
currentSize | number | 当前占用的空间大小, 单位 KB |
limitSize | number | 限制的空间大小,单位 KB |
使用案例
wx.getStorageInfo({
success(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})
6 Object wx.getStorageInfoSync()
与wx.getStorageInfo作用相同,此为同步版本
Object object
属性 | 类型 | 说明 |
---|---|---|
keys | Array.<string> | 当前 storage 中所有的 key |
currentSize | number | 当前占用的空间大小, 单位 KB |
limitSize | number | 限制的空间大小,单位 KB |
使用案例
try {
const res = wx.getStorageInfoSync()
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
} catch (e) {
// 不能正常获取时的逻辑
}
7 wx.removeStorage(Object object)
异步从本地缓存中移除指定 key
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
key | string | 是 | 本地缓存中指定的 key | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
使用案例
wx.removeStorage({
key: 'name',
success(res) {
console.log(res.data)
}
})
8 wx.removeStorageSync(string key)
wx.removeStorage的同步版本
string key
本地缓存中指定的 key
使用案例
try {
wx.removeStorageSync('name')
} catch (e) {
// 不能正常删除时的业务逻辑
}
9 wx.clearStorage(Object object)
异步清理本地数据缓存
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
使用案例
wx.clearStorage()
10 wx.clearStorageSync()
wx.clearStorage的同步版本
使用案例
try {
wx.clearStorageSync()
} catch (e) {
// 不能正常删除时的业务逻辑
}