uni-app的数据缓存

        在开发uni-app时,在项目中使用了本地存储和会话存储,在浏览器中运行是可以使用的,打打包到手机上本地存储还是可以用的,但会话存储不能使用。

        关于uni-app数据缓存的官方文档:uni.setStorage(OBJECT) @setstorage | uni-app官网

        在uni-app中数据缓存有两种:uni.setStorage(obj)  和  uni.setStorageSync(key, data),区别在于setStorage是异步接口,传入一个对象,setStorageSync是同步接口,传入键值对。

uni.setStorage(obj)要传一个对象

uni.setStorage({
  key: "token",
  data: res.token,
  success: function () {
    console.log("success");
  },
});

取用uni.getStorage(obj)取,也要传入一个对象

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

同步方法,uni.setStorageSync(key, data)传入一个键值对

uni.setStorageSync('token', res.token)

取使用uni.getStorageSync(key),传入key值

const token = uni.getStorageSync("token");
console.log(token);

        还有一些其他关于数据存储的方法,异步方法传入对象,同步方法传入键值对。

获取当前Storage的相关信息

uni.getStorageInfo()

uni.getStorageInfoSync()

从本地缓存中移除指定的key

uni.removeStorage()

uni.removeStorageSync()

清理本地数据缓存

uni.clearStorage()

uni.clearStorageSync()

猜你喜欢

转载自blog.csdn.net/h360583690/article/details/129888581