uni-app数据缓存

前言:

       整理下uni-app的资料,这里分享下它的数据缓存,uni-app中是没有cookie,session等内容的,这个跟我们的小程序很像。

目录:

官方入口:

具体使用:

方法1:

存指定内容:uni.setStorage(OBJECT)

取指定内容:uni.getStorage(OBJECT)

从缓存中移除指定内容:uni.removeStorage(OBJECT)

清除所有的缓存:uni.clearStorage()

方法二:异步

存:uni.setStorageSync(KEY,DATA)

获取指定内容:uni.getStorageSync(KEY)

移除:uni.removeStorageSync(KEY)

清除所有:uni.clearStorageSync()


官方入口:uni-app官网

具体使用:

方法1:

存指定内容uni.setStorage(OBJECT)

uni.setStorage({
    key: 'storage_key',
    data: 'hello',
    success: function () {
        console.log('success');
    }
});

OBJECT 参数说明

参数名类型必填说明
keyString本地缓存中的指定的 key
dataAny需要存储的内容,只支持原生类型、及能够通过 JSON.stringify 序列化的对象
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

取指定内容uni.getStorage(OBJECT)

uni.getStorage({
    key: 'storage_key',
    success: function (res) {
        console.log(res.data);
    }
});

OBJECT 参数说明

参数名类型必填说明
keyString本地缓存中的指定的 key
successFunction接口调用的回调函数,res = {data: key对应的内容}
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明

参数类型说明
dataAnykey 对应的内容

从缓存中移除指定内容uni.removeStorage(OBJECT)

uni.removeStorage({
    key: 'storage_key',
    success: function (res) {
        console.log('success');
    }
});

OBJECT 参数说明

参数名类型必填说明
keyString本地缓存中的指定的 key
successFunction接口调用的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

清除所有的缓存uni.clearStorage()

uni.clearStorage();

方法二:异步

存:uni.setStorageSync(KEY,DATA)

try {
    uni.setStorageSync('storage_key', 'hello');
} catch (e) {
    // error
}

获取指定内容:uni.getStorageSync(KEY)

try {
    const value = uni.getStorageSync('storage_key');
    if (value) {
        console.log(value);
    }
} catch (e) {
    // error
}

移除:uni.removeStorageSync(KEY)

try {
    uni.removeStorageSync('storage_key');
} catch (e) {
    // error
}

清除所有:uni.clearStorageSync()

try {
    uni.clearStorageSync();
} catch (e) {
    // error
}

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值