微信小程序数据缓存
有时需要把一些数据保存在手机本地中,这时就需要使用数据缓存的功能了。注意,微信小程序的数据缓存最大为1M。数据缓存接口分为同步接口和异步接口,同步接口会在名称的最后添加Sync。例如,从本地移除指定的key异步接口为wx.removeStorage()
,同步接口为wx.removeStorageSync()
。下面只讲解异步API。
设置本地缓存的key:
// index.js
wx.setStorage({
key: 'key, // 缓存的key名称
data: 'value', // 缓存key对应的内容
})
获取本地缓存中指定的key:
// index.js
var value = wx.getStorage('key'); // 设置缓存时的key
移除本地缓存中指定的key:
// index.js
wx.removeStorage({
key: 'key', // 要移除的key的名称
success: funcation(){
// 移除成功
},
fail: funcation(){
// 移除失败
},
complete: funcation(){
// 不管成功失败都会执行
}
})
清除所有的缓存数据:
// index.js
wx.clearStorage({
success: funcation(){
// 清除成功
},
fail: funcation(){
// 清除失败
},
complete: funcation(){
// 不管成功失败都会执行
}
})
酷客网相关文章:
评论前必须登录!
注册