uniapp数据缓存(存储/获取/移出)

1.存储:

异步:uni.setStorage(OBJECT)
uni.setStorage({
	key: 'storage_key',
	data: 'hello',
	success: function () {
		console.log('success');
	}
});
同步:uni.setStorageSync(KEY,DATA)
try {
	uni.setStorageSync('storage_key', 'hello');
} catch (e) {
	// error
}

2.获取:

异步:uni.getStorage(OBJECT)
uni.getStorage({
	key: 'storage_key',
	success: function (res) {
		console.log(res.data);
	}
});
同步:uni.getStorageSync(KEY)
try {
	const value = uni.getStorageSync('storage_key');
	if (value) {
		console.log(value);
	}
} catch (e) {
	// error
}

3.移出:

异步:uni.removeStorage(OBJECT)
uni.removeStorage({
	key: 'storage_key',
	success: function (res) {
		console.log('success');
	}
});
同步:uni.removeStorageSync(KEY)
try {
	uni.removeStorageSync('storage_key');
} catch (e) {
	// error
}

4.清除本地缓存

异步:uni.clearStorage()

uni.clearStorage();
同步:uni.clearStorageSync()
try {
	uni.clearStorageSync();
} catch (e) {
	// error
}

注:

异步 API在调用该方法后会立即返回,并且可能不会立即将数据存储到本地存储空间中,而是需要等待一定的时间。因此,在调用该方法后如果需要使用该数据,需要在回调函数中进行操作。

同步 API,调用该方法后会立即阻塞并等待数据存储完成,返回的是存储操作的结果。可以简化代码,避免使用回调函数。但是,需要注意同步 API 可能会阻塞主线程,因此应该尽量避免在 UI 线程中使用同步 API。

同步异步详细解答可跳转--> 同步,异步及Promise的详解_promise 同步执行-CSDN博客

获取详细信息点击官方文档->uni.setStorage(OBJECT) @setstorage | uni-app官网

相关推荐

  1. uniapp数据缓存存储/获取/

    2023-12-19 10:26:02       72 阅读
  2. uniapp中使用LocalStorage实现本地存储缓存数据

    2023-12-19 10:26:02       45 阅读
  3. uniapp vue 获取天气数据

    2023-12-19 10:26:02       28 阅读
  4. uniapp使用数据持久化存储

    2023-12-19 10:26:02       30 阅读
  5. 鼠标事件

    2023-12-19 10:26:02       57 阅读
  6. 69、FIFO缓存发送数据(先入先

    2023-12-19 10:26:02       44 阅读
  7. uniapp读取(获取)缓存中的对象值(微信小程序)

    2023-12-19 10:26:02       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-19 10:26:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-19 10:26:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-19 10:26:02       82 阅读
  4. Python语言-面向对象

    2023-12-19 10:26:02       91 阅读

热门阅读

  1. ubuntu添加路由

    2023-12-19 10:26:02       58 阅读
  2. python爬虫---urllib

    2023-12-19 10:26:02       62 阅读
  3. Wireshark在云计算中的应用

    2023-12-19 10:26:02       46 阅读
  4. flutter学习-day14-事件处理和通知

    2023-12-19 10:26:02       47 阅读
  5. C# 获取Excel里引用的外部其他excel文件清单

    2023-12-19 10:26:02       52 阅读