Table of contents
一.uni.setStorage(OBJECT)
Storing data in the specified key in the local cache will overwrite the original content corresponding to the key, which is an asynchronous interface.
OBJECT parameter
parameter name | illustrate |
---|---|
key | The specified key in the local cache (required) |
data | The content to be stored only supports native types and objects that can be serialized through JSON.stringify (required) |
success | Callback function for successful interface call |
fail | Callback function for interface call failure |
complete | The callback function of the end of the interface call (the call will be executed successfully or failed) |
//将数据存入storage
setInfo(){
uni.setStorage({
//存入Storage
key:'userInfo', //自己取个名字
data: {
//存的数据可以是很多条
'uid': res.data.uid,
'uname':res.data.uname,
'phone': res.data.phone
}
success() {
console.log('userInfo储存成功');
}
})
}
uni.getStorage()
//从Storage取数据
getInfo(){
uni.getStorage({
key:'userInfo',
success(res) {
console.log('获取成功',res.data);
}
})
}
uni.removeStorage()
//清除Storage
removeInfo(){
uni.removeStorage({
//删除Storage
key:'userInfo',
success() {
console.log('删除成功');
}
})
},
二. uni.setStorageSync(KEY,DATA)
Storing data in the specified key in the local cache will overwrite the original content corresponding to the key, which is a synchronous interface.
Parameter Description
parameter | illustrate |
---|---|
key | The specified key in the local cache |
data | The content that needs to be stored only supports native types and objects that can be serialized through JSON.stringify |
//存
uni.setStorageSync('storage_key', 'hello');
uni.getStorageSync()
//从本地缓存中同步获取指定 key 对应的内容
const value = uni.getStorageSync('storage_key');
console.log(value);
uni.removeStorageSync()
//从本地缓存中同步移除指定 key
uni.removeStorageSync('storage_key');
Notice
1. Each applet has its own storage api, and the data storage life cycle is consistent with the applet itself, that is, unless the user actively deletes or is automatically cleared after a certain period of time, the data is always available.
2. The maximum length of data that can be stored in a single key of the WeChat Mini Program is 1MB, and the upper limit of all data storage is 10MB.
reference
Vue3 official document
Shang Silicon Valley Vue3 video