

English
app、小程序、web,均提供了方便的key-value模式的本地数据存储,通过键值对的方式存取数据。
uni-app的Storage在不同端的实现不同:
注意
uni-
、uni_
、dcloud-
、dcloud_
为前缀的key,为系统保留关键前缀。如uni_deviceId
、uni_id_token
,请开发者为key命名时避开这些前缀。uni.setStorage函数定义 将数据存储在本地storage存储中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
name | type | required | default | description |
---|---|---|---|---|
options | SetStorageOptions | YES | - | uni.setStorage参数定义 |
name | type | optinal | default | description |
---|---|---|---|---|
key | string | YES | - | 本地存储中的指定的 key |
data | any | YES | - | 需要存储的内容,只支持原生类型、及能够通过 JSON.stringify 序列化的对象 |
success | (res: SetStorageSuccess) => void | NO | - | 接口调用成功的回调函数 |
fail | (res: UniError) => void | NO | - | 接口调用失败的回调函数 |
complete | (res: any) => void | NO | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.setStorageSync函数定义 将 data 存储在本地storage存储中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
name | type | required | default | description |
---|---|---|---|---|
key | string | YES | - | 本地storage存储中的指定的 key |
data | any | YES | - | 需要存储的内容,只支持原生类型、及能够通过 JSON.stringify 序列化的对象 |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.getStorage函数定义 从本地存储中异步获取指定 key 对应的内容。
name | type | required | default | description |
---|---|---|---|---|
options | GetStorageOptions | YES | - | uni.getStorage参数定义 |
name | type | optinal | default | description |
---|---|---|---|---|
key | string | YES | - | 本地存储中的指定的 key |
success | (res: GetStorageSuccess) => void | NO | - | 接口调用成功的回调函数 |
fail | (res: UniError) => void | NO | - | 接口调用失败的回调函数 |
complete | (res: any) => void | NO | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
name | type | optinal | default | description |
---|---|---|---|---|
data | any | NO | - | key 对应的内容 |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.getStorageSync函数定义 从本地存储中同步获取指定 key 对应的内容。
name | type | required | default | description |
---|---|---|---|---|
key | string | YES | - | 本地存储中的指定的 key |
Type | optinal |
---|---|
any | NO |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.getStorageInfo函数定义 异步获取当前 storage 的相关信息。
name | type | required | default | description |
---|---|---|---|---|
options | GetStorageInfoOptions | YES | - | uni.getStorageInfo参数定义 |
name | type | optinal | default | description |
---|---|---|---|---|
success | (res: GetStorageInfoSuccess) => void | NO | - | 接口调用成功的回调函数 |
fail | (res: UniError) => void | NO | - | 接口调用失败的回调函数 |
complete | (res: any) => void | NO | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
name | type | optinal | default | description |
---|---|---|---|---|
keys | Array<string> | YES | - | 当前 storage 中所有的 key |
currentSize | number | YES | - | 当前占用的空间大小, 单位:kb |
limitSize | number | YES | - | 限制的空间大小, 单位:kb |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.getStorageInfoSync函数定义 同步获取当前 storage 的相关信息。
Type |
---|
GetStorageInfoSuccess |
name | type | optinal | default | description |
---|---|---|---|---|
keys | Array<string> | YES | - | 当前 storage 中所有的 key |
currentSize | number | YES | - | 当前占用的空间大小, 单位:kb |
limitSize | number | YES | - | 限制的空间大小, 单位:kb |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.removeStorage函数定义 从本地存储中异步移除指定 key。
name | type | required | default | description |
---|---|---|---|---|
options | RemoveStorageOptions | YES | - | uni.removeStorage参数定义 |
name | type | optinal | default | description |
---|---|---|---|---|
key | string | YES | - | 本地存储中的指定的 key |
success | (res: RemoveStorageSuccess) => void | NO | - | 接口调用的回调函数 |
fail | (res: UniError) => void | NO | - | 接口调用失败的回调函数 |
complete | (res: any) => void | NO | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.removeStorageSync函数定义 从本地存储中同步移除指定 key。
name | type | required | default | description |
---|---|---|---|---|
key | string | YES | - | 本地存储中的指定的 key |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.clearStorage函数定义 清除本地数据存储。
name | type | required | default | description |
---|---|---|---|---|
option | ClearStorageOptions | NO | - | - |
name | type | optinal | default | description |
---|---|---|---|---|
success | (res: ClearStorageSuccess) => void | NO | - | 接口调用的回调函数 |
fail | (res: UniError) => void | NO | - | 接口调用失败的回调函数 |
complete | (res: any) => void | NO | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
uni.clearStorageSync函数定义 清除本地数据存储。
Android version | Android uni-app | Android uni-app-x | iOS version | iOS uni-app | iOS uni-app-x |
---|---|---|---|---|---|
4.4.4 | 2.0.3 | 3.9.0 | 9.0 | 2.0.3 | 3.9.0 |
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1">
<!-- #endif -->
<view>
<page-head :title="title"></page-head>
<view class="uni-common-mt">
<view class="uni-list">
<view class="uni-list-cell uni-list-cell-line">
<view class="uni-list-cell-left">
<view class="uni-label">key</view>
</view>
<view class="uni-list-cell-db">
<input
class="uni-input"
type="text"
placeholder="请输入key"
name="key"
:value="key"
@input="keyChange"
/>
</view>
</view>
<view class="uni-list-cell">
<view class="uni-list-cell-left">
<view class="uni-label">value</view>
</view>
<view class="uni-list-cell-db">
<input
class="uni-input"
type="text"
placeholder="请输入value"
name="data"
:value="data"
@input="dataChange"
/>
</view>
</view>
</view>
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<button class="uni-btn btn-getStorageInfoASync" type="primary" @tap="getStorageInfo">
获取存储概述-异步
</button>
<button class="uni-btn btn-getStorageInfoSync" @tap="getStorageInfoSync">
获取存储概述-同步
</button>
</view>
<text>{{ storageInfo }}</text>
<view class="uni-flex uni-row">
<button
type="default"
style="width:325rpx"
@tap="strMock"
>
填充字符串
</button>
<button
type="default"
style="width:325rpx"
@tap="complexMock"
>
填充复杂对象
</button>
</view>
<view class="uni-flex uni-row">
<button
type="default"
style="width:325rpx"
@tap="numberMock"
>
填充整型
</button>
<button
type="default"
style="width:325rpx"
@tap="floatMock"
>
填充浮点型
</button>
</view>
</view>
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<button
type="primary"
class="uni-btn btn-setstorageAsync"
@tap="setStorage"
>
存储数据-异步
</button>
<button class="uni-btn btn-getstorageAsync" @tap="getStorage">读取数据-异步</button>
<button class="uni-btn btn-removeStorageInfoASync" @tap="removeStorage">移除数据-异步</button>
<button class="uni-btn btn-clearStorageInfoASync" @tap="clearStorage">清理数据-异步</button>
</view>
<view class="uni-btn-v">
<button
type="primary"
class="uni-btn btn-setstorageSync"
@tap="setStorageSync"
>
存储数据-同步
</button>
<button class="uni-btn btn-getstorageSync" @tap="getStorageSync">读取数据-同步</button>
<button class="uni-btn btn-removeStorageInfoSync" @tap="removeStorageSync">
移除数据-同步
</button>
<button class="uni-btn btn-clearStorageInfoSync" @tap="clearStorageSync">
清理数据-同步
</button>
</view>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script lang="uts">
export default {
data() {
return {
title: 'get/set/clearStorage',
key: '',
data: '' as any,
apiGetData:'' as any|null,
storageInfo: '',
}
},
methods: {
getStorageInfo() {
uni.getStorageInfo({
success: (res) => {
this.apiGetData = res
this.storageInfo = JSON.stringify(res)
},
})
},
getStorageInfoSync() {
try {
const res = uni.getStorageInfoSync()
this.apiGetData = res
this.storageInfo = JSON.stringify(res)
} catch (e) {
// error
console.log(e)
}
},
strMock() {
this.key = 'key_' + Math.random()
this.data = '测试字符串数据,长度为16个字符'
},
complexMock() {
this.key = 'key_' + Math.random()
let jsonObj = {
name: '张三',
age: 12,
classMate: [
{
id: 1001,
name: '李四',
},
{
id: 1002,
name: 'jack ma',
},
],
}
this.data = jsonObj
},
numberMock() {
this.key = 'key_' + Math.random()
this.data = 10011
},
floatMock() {
this.key = 'key_' + Math.random()
this.data = 3.1415926535893384626
},
keyChange: function (e: InputEvent) {
this.key = e.detail.value
},
dataChange: function (e: InputEvent) {
this.data = e.detail.value
},
getStorage: function () {
var key = this.key
if (key.length == 0) {
uni.showModal({
title: '读取数据失败',
content: 'key 不能为空',
showCancel: false,
})
} else {
let that = this
uni.getStorage({
key: key,
success: (res) => {
that.apiGetData = res.data
let desc:string = typeof this.apiGetData
if("object" == desc){
desc = desc + ": " + JSON.stringify(this.apiGetData)
}else{
desc = desc + ": " + this.apiGetData
}
uni.showModal({
title: '读取数据成功',
content: desc,
showCancel: false,
})
},
fail: () => {
uni.showModal({
title: '读取数据失败',
content: '找不到 key 对应的数据',
showCancel: false,
})
},
})
}
},
getStorageSync: function () {
var key = this.key
if (key.length == 0) {
uni.showModal({
title: '读取数据失败',
content: 'key 不能为空',
showCancel: false,
})
} else {
this.apiGetData = uni.getStorageSync(key)
let desc:string = typeof this.apiGetData
if("object" == desc){
desc = desc + ": " + JSON.stringify(this.apiGetData)
}else{
desc = desc + ": " + this.apiGetData
}
uni.showModal({
title: '读取数据成功',
content: desc,
showCancel: false,
})
}
},
setStorage: function () {
var key = this.key
var data = this.data
if (key.length == 0) {
uni.showModal({
title: '保存数据失败',
content: 'key 不能为空',
showCancel: false,
})
} else {
uni.setStorage({
key: key,
data: data,
success: () => {
uni.showModal({
title: '存储数据成功',
showCancel: false,
})
},
fail: () => {
uni.showModal({
title: '储存数据失败!',
showCancel: false,
})
},
})
}
},
setStorageSync: function () {
var key = this.key
var data = this.data
if (key.length == 0) {
uni.showModal({
title: '保存数据失败',
content: 'key 不能为空',
showCancel: false,
})
} else {
uni.setStorageSync(key, data)
uni.showModal({
title: '存储数据成功',
showCancel: false,
})
}
},
removeStorage: function () {
uni.removeStorage({
key: this.key,
success: () => {
uni.showModal({
title: '移除数据成功',
showCancel: false,
})
},
fail: () => {
uni.showModal({
title: '移除数据失败',
showCancel: false,
})
},
})
},
removeStorageSync: function () {
uni.removeStorageSync(this.key)
uni.showModal({
title: '移除数据成功',
showCancel: false,
})
},
clearStorage: function () {
this.key = ''
this.data = ''
uni.clearStorage({
success: function (_) {
uni.showModal({
title: '清除数据成功',
showCancel: false,
})
},
fail: function (_) {
uni.showModal({
title: '清除数据失败',
showCancel: false,
})
},
})
},
clearStorageSync: function () {
this.key = ''
this.data = ''
uni.clearStorageSync()
uni.showModal({
title: '清除数据成功',
content: ' ',
showCancel: false,
})
},
},
}
</script>
<style>
.btn-setstorage {
background-color: #007aff;
color: #ffffff;
}
.button-sp-area {
flex-direction: row;
margin: 0 auto;
}
</style>
name | type | optinal | default | description |
---|---|---|---|---|
errMsg | string | YES | - | 错误信息 |