• 小程序---微信本地存储的方法使用


    我们在开发的过程中,常常会用到本地存储,下面我给大家分享一下微信本地存储的使用。

    一、数据支持

    需要存储的内容。只支持原生类型、Date、及能够通过JSON.stringify序列化的对象。

    二、方法

    1、同步

    (1wx.setStorageSync();  //存储值

    1 try {
    2   wx.setStorageSync('key', 'value')
    3 } catch (e) {
    4 
    5  }

    (2wx.removeStorageSync();   // 移除指定的值

    1 try {
    2   wx.removeStorageSync('key')
    3 } catch (e) {
    4   // Do something when catch error
    5 }

    3wx.getStorageSync();  // 获取值

    1 try {
    2   var value = wx.getStorageSync('key')
    3   if (value) {
    4     // Do something with return value
    5   }
    6 } catch (e) {
    7   // Do something when catch error
    8 }

    4wx.getStorageInfoSync();  // 获取当前 storage 中所有的 key

    1 try {
    2   const res = wx.getStorageInfoSync()
    3   console.log(res.keys)
    4   console.log(res.currentSize)
    5   console.log(res.limitSize)
    6 } catch (e) {
    7   // Do something when catch error
    8 }

    5wx.clearStorageSync();  // 清除所有的key

    1 try {
    2   wx.clearStorageSync()
    3 } catch(e) {
    4   // Do something when catch error
    5 }

    2、异步

    (1wx.setStorage();  //存储值

    将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。数据存储生命周期跟小程序本身一致,即除用户主动删除或超过一定时间被自动清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB

    1 wx.setStorage({
    2   key:"key",
    3   data:"value"})

    (2wx.removeStorage();   // 移除指定的值

    1 wx.removeStorage({
    2   key: 'key',
    3   success (res) {
    4     console.log(res)
    5   }})

    3wx.getStorage();  // 获取值

    1 wx.getStorage({
    2   key: 'key',
    3   success (res) {
    4     console.log(res.data)
    5   }})

    4wx.getStorageInfo();  // 获取当前 storage 中所有的 key

    1 wx.getStorageInfo({
    2   success (res) {
    3     console.log(res.keys)
    4     console.log(res.currentSize)
    5     console.log(res.limitSize)
    6   }})

    5wx.clearStorage();  // 清除所有的key

    1 wx.clearStorage()

      以上就是微信给我们提供的本地存储的方法,希望对大家有帮助!

  • 相关阅读:
    css浮动布局小技巧
    dhtml
    js 图片轮播
    css 简单梯形
    css 平行四边
    activity添加切换动画之后出现的黑色背景问题
    Android 设置ImageView宽度固定,其高度按比例缩放适应
    白底黑字!Android浅色状态栏黑色字体模式(另)
    Adroid ViewPage+GridView实现每页6个元素,三页滑动切换
    Android View转换成图片保存
  • 原文地址:https://www.cnblogs.com/le220/p/10963911.html
Copyright © 2020-2023  润新知