微信小程序之缓存——不同页面传递数据

1. 添加缓存

单个密钥允许存储的最大数据长度为1MB,所有数据存储上限为10MB。

 // 存储信息到storage 
// 异步存储
set() {
    wx.setStorage({
        key: 'user',
        data: 'cck',
        success: ()=> {
            console.log('存储成功');
        }
    })
},

// 同步存储
set() {
    try {
        wx.setStorageSync('user', 'cck')
    } catch (e) { }
}

2. 获取缓存

从本地缓存中异步获取指定key的内容

// 异步
wx.getStorage({
  key: 'user',
  success (res) {
    console.log(res.data)
  }
})

// 同步
try {
  var value = wx.getStorageSync('user')
  if (value) {
    // Do something with return value
  }
} catch (e) {
  // Do something when catch error
}

3. 移除缓存

从本地缓存中移除指定 key

// 异步
wx.removeStorage({
  key: 'user',
  success (res) {
    console.log(res.data)
  }
})

// 同步
try {
  wx.removeStorageSync('user')
} catch (e) {
  // Do something when catch error
}
posted @ 2018-11-23 16:46  Mr.曹  阅读(4160)  评论(0编辑  收藏  举报