微信小程序中 數據緩存


微信小程序數據緩存

微信官方文檔   介紹鏈接:https://developers.weixin.qq.com/miniprogram/dev/api/storage/wx.getStorage.html

 

  • 和瀏覽器緩存 對比

    • 瀏覽器:  

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

      • sessionstorage  (僅在當前會話下有效,關閉頁面或瀏覽器后被清除)  

    • 小程序

      • storage

  • 作用:也是用來緩存數據

  • 使用:

    • 存儲

      • 異步存儲

        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 中可以存儲任意類型的數據  對象、數組、、、等等。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM