一,全局变量
(1)在app.js设置globalData的变量
App({
onLaunch: function () {
this.globalData.id = "51"
},
globalData: {
id: null,
name:null,
passwd:null
}
})
(2)在使用页面获得保存参数
1,获取应用实例;2,获得实例变量
const app = getApp()
Page({
data: {
id: null
},
onLoad: function () {
var buffer=app.globalData.id;
this.setData({
id: buffer
})
}
})
二,setStorage
将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
key | String | 是 | 本地缓存中的指定的 key |
data | Object/String | 是 | 需要存储的内容 |
success | Function | 否 | 接口调用成功的回调函数 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.setStorage({
key:"key",
data:"value"
})
三,getStorag
从本地缓存中异步获取指定 key 对应的内容。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
key | String | 是 | 本地缓存中的指定的 key |
success | Function | 否 | 接口调用的回调函数,res = {data: key对应的内容} |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.getStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})
四,getStorageInfo
异步获取当前storage的相关信息
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
success | Function | 是 | 接口调用的回调函数,详见返回参数说明 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数说明:
参数 | 类型 | 说明 |
---|---|---|
keys | String Array | 当前storage中所有的key |
currentSize | Number | 当前占用的空间大小, 单位kb |
limitSize | Number | 限制的空间大小,单位kb |
示例代码:
wx.getStorageInfo({
success: function(res) {
console.log(res.keys)
console.log(res.currentSize)
console.log(res.limitSize)
}
})
五,removeStorage
从本地缓存中异步移除指定 key 。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
key | String | 是 | 本地缓存中的指定的 key |
success | Function | 否 | 接口调用的回调函数 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.removeStorage({
key: 'key',
success: function(res) {
console.log(res.data)
}
})
六,clearStorage()
清理本地数据缓存。
wx.clearStorage()
版权声明:本文为wzjisking原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。