数据缓存
1.uni.setStorage
将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
代码演示
缓存代码:
setStor () {
uni.setStorage({
key: 'id',
data: 100,
success () {
console.log('存储成功')
}
})
}
2.uni.setStorageSync
将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
代码演示
setStor () {
uni.setStorageSync('id',100)
}
3.uni.getStorage
从本地缓存中异步获取指定 key 对应的内容。
代码演示
代码:
uni.getStorage({
key: 'id',
success: res=>{
this.id = res.data
}
})
}
}
4.uni.getStorageSync
从本地缓存中同步获取指定 key 对应的内容。
代码演示
代码:
getStorage () {
const id = uni.getStorageSync('id')
console.log(id)
}
5.uni.removeStorage
从本地缓存中异步移除指定 key。
代码演示
代码:
removeStorage () {
uni.removeStorage({
key: 'id',
success: function () {
console.log('删除成功')
}
})
}
}
6.uni.removeStorageSync
从本地缓存中同步移除指定 key。
代码演示
代码:
removeStorage () {
uni.removeStorageSync('id')
}