微信小程序 wx.setstoragesync和wx.setstorage 区别

相同点:

微信小程序 wx.setstoragesync和wx.setstorage都是能把值保存在微信小程序缓存中,类似于浏览器的localstorage概念

区别:

wx.setStorage是异步的:就是这个在执行中不会影响其他代码的执行

wx.setStorageSync是同步的:要等待这个代码执行完才会去执行其他的代码

使用范例:

setStorage:

// 设置值
wx.setStorage({
  key:"key",
  data:"value"
})
// 取值
wx.getStorage({
  key: 'key',
  success (res) {
    console.log(res.data)
  }
})

StorageSync:

// 设置值
wx.setStorageSync('key', 'value')
// 取值
var value = wx.getStorageSync('key')

猜你喜欢

转载自blog.csdn.net/web_ding/article/details/122984895