欢迎光临,了解微信小程序开发,就上易用通!

Charlotte微信小程序开发(一)--数据存储

发布:2018-04-23 12:14浏览: 来源:网络 作者:cola

一.异步存储


(1)wx.setStorage(OBJECT)

将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
wx.setStorage({
  key:"key",
  data:"value",
  success:function(res){
      console.log(res);    //接口调用成功的回调函数
  },
  fail:function(res){
      console.log(res);    //接口调用失败的回调函数
  },
  complete:function(res){
      console.log(res);    //接口调用结束的回调函数(不论失败还是成功)
  }

})
  •  

(2)wx.getStorage(OBJECT)

从本地缓存中异步获取指定 key 对应的内容。
wx.getStorage({
  key:"key",
  success:function(res){
      console.log(res);    //接口调用成功的回调函数
  },
  fail:function(res){
      console.log(res);    //接口调用失败的回调函数
  },
  complete:function(res){
      console.log(res);    //接口调用结束的回调函数(不论失败还是成功)
  }

})

二. 同步存储


(1)wx.setStorageSync(KEY,DATA)

    将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
wx.setStorageSync('key', 'value')
  • 1
  • 1

(2)wx.getStorageSync(KEY)

    从本地缓存中同步获取指定 key 对应的内容。
wx.getStorageSync('key')
  • 1
  • 1

三. 数据删除  (1)wx.removeStorage(OBJECT)

    从本地缓存中异步移除指定 key 。
wx.removeStorage({
  key: 'key',
  success: function(res) {
    console.log(res.data)
  },
  fail: function(res) {
    console.log(res.data)
  },
  complete: function(res) {
    console.log(res.data)
  }
})

(2) wx.removeStorageSync(KEY)

  从本地缓存中同步移除指定 key 。
wx.removeStorageSync('key')
  • 1
  • 1

(3) wx.clearStorage()

    清理本地数据缓存。
wx.clearStorage()
  • 1
  • 1

(4) wx.clearStorageSync()

同步清理本地数据缓存
wx.clearStorageSync()





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。