uniapp的uni.setStorage存储数组对象

作者: gavin 分类: uniapp 发布时间: 2022-01-28 11:39

单一key值,存储一个JSON.stringify 序列化的对象(简称:json对象)

常规写法:

//存储字符串
uni.setStorage({ 'storage_key_AA': 'aa' });
uni.setStorage({ 'storage_key_BB': 'bb' });
//获取存储值
const valueA = uni.getStorageSync('storage_key_AA');
const valueB = uni.getStorageSync('storage_key_BB');
//读取存储对象对应的值
console.log(valueA);
console.log(valueB);

存储数组对象写法:

//存储对象
uni.setStorage({
   key: 'storage_key',
   data: {
    AA: 'aa',
    BB: 'bb'
   }
});

//获取存储值
const value = uni.getStorageSync('storage_key');

//判断当前是否有该存储值
if (value) {
  //读取存储对象对应的值
   console.log(value.AA);
   console.log(value.BB);
}

拓展:uniapp 对应内容的API
https://uniapp.dcloud.io/api/storage/storage?id=setstorage