微信小程序中 数据缓存

微信小程序数据缓存

微信官方文档   介绍链接:https://developers.weixin.qq.com/miniprogram/dev/api/storage/wx.getStorage.html

  • 和浏览器缓存 对比

    • 浏览器:  

      • localstorage   (除非手动被清除,否则永久保存在本地)

      • sessionstorage  (仅在当前会话下有效,关闭页面或浏览器后被清除)  

    • 小程序

      • storage

  • 作用:也是用来缓存数据

  • 使用:

    扫描二维码关注公众号,回复: 7181957 查看本文章
    • 存储

      • 异步存储

        wx.setStorage({
        key: '',
        data: ''
        })
      • 同步存储

        wx.setStorageSync(key, data)
    • 取值

      • 异步取值

        wx.getStorage({
        key: '',
        success: res => {
        console.log(res.data)
        }
        })
      • 同步取值

        let res = wx.getStorageSync(key)
    • 清除

      • 异步清除

        // 清除的指定 key 中的内容
        wx.removeStorage({
        key: '',
        success: res => {
        console.log(res.data)
        }
        })
        // 清除全部的 storage
        wx.clearStorage()
      • 同步清除

        // 清除的指定 key 中的内容
        wx.removeStorageSync(key)
        // 清除全部的 storage
        wx.clearStorageSync()
    • 总结:

      • 小程序中的storage 与 浏览器中的 localstorage 用户比较相似,可以参数着理解

      • 注意:

        • localstorage 中只能存储字符串

        • storage 中可以存储任意类型的数据  对象、数组、、、等等。

猜你喜欢

转载自www.cnblogs.com/KoBe-bk/p/11465354.html