微信小程序 wx:setStorage和wx:getStorage 数据存取

1、wx:setStorage() (uniapp写法,uni.setStorage)

将数据存储在本地缓存中指定的key中,它会覆盖掉原来该key 对应的内容。

数据存储生命周期跟小程序本身一致,即除用户主动删除或超过一定时间被自动清理。 否则数据一直可用。 单个 key允许存储的最大数据长度为1MB,  所有数据存储上限为 10MB。

示例:

  myset01(){
    wx.setStorage({
      data: "小程序项目开发",
      key: '01',
      success(res){
          console.log("==setStorage成功  res=="+JSON.stringify(res));
      }
    })
  },

 

2、wx.getStroage()   (uniapp写法,uni.getStroage)

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

示例:

  myget01(){
    var that = this;
    wx.getStorage({
      key: '01',
      success(res){
        that.setData({
          storage_data: res.data,
        })
      }
    })
  },

 

3、wx.removeStorage()   (uniapp写法,uni.removeStorage)

从本地缓存中移除指定的key

示例:

  myremove01(){
     wx.removeStorage({
       key:'01',
       success(res){
         console.log("remove key 成功");
       },fail(res){
       }
     })
  },

 

4、wx.clearStorage()   (uniapp写法,uni.clearStorage)

清空所有缓存数据。

示例:

  myclear01(){
    wx.clearStorage({
      success(res){
        console.log("clearStorage 成功");
      },
    })
  },

 

posted @ 2022-07-08 11:41  蓦然JL  阅读(1516)  评论(0编辑  收藏  举报
访问主页
关注我
关注微博
私信我
返回顶部