How to save setstorage in wechat applet

Time:2021-6-24

This article mainly introduces how to save setstorage in wechat applet. The example code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

Save setstorage in JS. Key is your key and data is your value

wx.setStorage({
   key: 'mm',
   data: 1,
  })
  //How to get getstoragesync
 
  How to get console.log (Wx. Getstoragesync ('mm '))

Next is the access to data or data. It’s actually the same. Code directly

Code in JS file

onLoad: function (options) {
  var thit = this
  Wx. Request ({// request address)
   url: 'http://127.0.0.1:8000/api/index/',
   method: 'get',
   //Data: {}, // no data can be written 
   Header: {// request header
    'content-type': 'application/json'
    // "Content-Type": "application/x-www-form-urlencoded"
   },
   //If you write this directly in susess, it becomes this of Wx. Request()
   success: function (res) {
    //Res.data is equivalent to the data in Ajax, which is the data returned by the background
    //Print the data returned by the background
    console.log(res.data.message)
    //Directly assign the data returned by the background to names, then you can call names directly
    thit.setData({
     names: res.data.message,
     Names1: res.data.message 1, // the second data returned
     Names2: res.data.message 2 // the third data
    })

  //Save to setstorage
    wx.setStorage({
     key: 'kkk',
     Data: res.data.message 2, // save the third data
    })
    //View mode defines a variable
      var eee = wx.getStorageSync('kkk')
    console.log(eee)
   }
  })
   },

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.