1. 程式人生 > >微信小程式的wx.getStorageSync中的key使用引數傳遞

微信小程式的wx.getStorageSync中的key使用引數傳遞

在使用wx.getStorageSync的時候,由於需要獲取到許多鍵對應的值,這個時候一開始就一個一個的獲取,最後發現wx.getStorageSync中是可以傳引數的,其中index頁面的內容是

<view>
  <input class='input' bindtap='click'>快取</input>
</view>
const app = getApp()

Page({
  data: {

  },
  onLoad: function () {
  
  },

  click:function(){
    wx.setStorageSync("game", "lol")
    wx.navigateTo({
      url: '/ceshi/ceshi',
    })
  }
})

然後ceshi頁面的內容是:

// ceshi/ceshi.js
Page({

  data: {

  },
  
  onLoad: function (options) {
    var b = this.get('game')
    console.log(b)
  },

  get:function(a){
    return wx.getStorageSync(a)
  }
})

其中在get函式中wx.getStorageSync中的key是從onLoad傳過來的,此時需要加上return返回,這樣才可以使用