uniapp本地存储分为同步和异步存储
异步和同步区别就是 异步不管缓存有没有成功都会继续往下走,同步等待成功之后才会往下走
uniapp同步本地缓存
- uni.setStorageSync(key, data):将数据同步保存在本地缓存中,key为字符串类型,表示要保存数据的键,data可以是任何支持JSON序列化的数据类型。
- uni.getStorageSync(key):从本地缓存中同步获取指定key的数据。
- uni.getStorageInfoSync() 同步获取当前 storage 的相关信息。
- uni.removeStorageSync(key):从本地缓存中同步移除指定key的数据。
- uni.clearStorageSync():清空本地缓存中所有数据。
1.uni.setStorageSync(key, data) 数据同步存储 和 获取
let items = JSON.stringify(数组或者对象);
uni.setStorageSync("data2" , items);
let data = uni.getStorageSync("data2" );
let items = JSON.parse(data)
存储
onShow(){
let obj = {
id:'1',
name:"wwww",
list:[{
id:'2',
name:"eeee",
list:[{
id:'3',
name:"tttt",
}]
}]
};
let item = JSON.stringify(obj);
uni.setStorageSync('objData', item);
},
更改(和存储一样,设置一样的名字第二次就会给第一次覆盖了)
change(){
let obj = {
id:'2',
name:"wwww",
list:[{
id:'3',
name:"eeee",
list:[{
id:'4',
name:"tttt",
}]
}]
};
let item = JSON.stringify(obj);
uni.setStorageSync('objData', item);
}
获取某个
get(){
let data = uni.getStorageSync('objData')
console.log(JSON.parse(data));
}
获取所有storage里面的信息:uni.getStorageInfoSync()
success 返回参数说明
参数 | 类型 | 说明 |
---|---|---|
keys | Array<String> | 当前 storage 中所有的 key |
currentSize | Number | 当前占用的空间大小, 单位:kb |
limitSize | Number | 限制的空间大小, 单位:kb |
const res = uni.getStorageInfoSync();
console.log(res.keys);
console.log(res.currentSize);
console.log(res.limitSize);
删除
del(){
uni.removeStorageSync('objData')
}
删除全部
delAll(){
uni.clearStorageSync()
}
uniapp 异步本地存储
- uni.setStorage(Object):将数据异步保存在本地缓存中
- uni.getStorage(Object):从本地缓存中异步获取指定key的数据
- uni.getStorageInfo(OBJECT) :异步获取当前 storage 的相关信息。
- uni.removeStorage(Object):从本地缓存中异步移除指定key的数据
- uni.clearStorage()清理本地数据缓存。
存储 // uni.setStorage(Object)
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
key | String | 是 | 本地缓存中的指定的 key |
data | Any | 是 | 需要存储的内容,只支持原生类型、及能够通过 JSON.stringify 序列化的对象 |
success | Function | 否 | 接口调用成功的回调函数 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
onShow(){
let obj = {
id:'1',
name:"wwww",
list:[{
id:'2',
name:"eeee",
list:[{
id:'3',
name:"tttt",
}]
}]
};
uni.setStorage({
key: 'storage_key',
data: obj,
success: function(){
console.log('success'); //成功
},
fail:function(){
console.log("fail"); //失败
},
complete:function(){
console.log("complete");//成功失败都调
}
});
},
获取 // uni.getStorage(Object)
OBJECT 参数说明
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
key | String | 是 | 本地缓存中的指定的 key |
success | Function | 是 | 接口调用的回调函数,res = {data: key对应的内容} |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success 返回参数说明
参数 | 类型 | 说明 |
---|---|---|
data | Any | key 对应的内容 |
get(){
uni.getStorage({
key: 'storage_key',
success: function (res) {
console.log(res.data);
}
});
},
获取当前storage里面的信息: uni.getStorageInfo(object)
OBJECT 参数说明
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
success | Function | 是 | 接口调用的回调函数,详见返回参数说明 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success 返回参数说明
参数 | 类型 | 说明 |
---|---|---|
keys | Array<String> | 当前 storage 中所有的 key |
currentSize | Number | 当前占用的空间大小, 单位:kb |
limitSize | Number | 限制的空间大小, 单位:kb |
uni.getStorageInfo({
success: function (res) {
console.log(res.keys);
console.log(res.currentSize);
console.log(res.limitSize);
}
});
移除 uni.removeStorage(object)
OBJECT 参数说明
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
key | String | 是 | 本地缓存中的指定的 key |
success | Function | 是 | 接口调用的回调函数 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例
uni.removeStorage({
key: 'storage_key',
success: function (res) {
console.log('success');
}
});
清除 uni.clearStorage()
示例
uni.clearStorage();
uni-app的Storage在不同端的实现不同:
- H5端为localStorage,浏览器限制5M大小,是缓存概念,可能会被清理
- App端为原生的plus.storage,无大小限制,不是缓存,是持久化的
- 各个小程序端为其自带的storage api,数据存储生命周期跟小程序本身一致,即除用户主动删除或超过一定时间被自动清理,否则数据都一直可用。
- 微信小程序单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
- 支付宝小程序单条数据转换成字符串后,字符串长度最大200*1024。同一个支付宝用户,同一个小程序缓存总上限为10MB。
- 非App平台清空Storage会导致uni.getSystemInfo获取到的deviceId改变