Micro letter applet caching Storage

Storage

Micro letter applet song list page implementation is implemented in the song list, because the list of songs included in the song information, so we will song list data stored in the cache, so that song playback page you do not need to re-request data, returning from the playback page song list page does not need to re-request data.

Just call the following method after the request is successful cloud function to
Here Insert Picture Description
all the code is as follows:

// pages/musiclist/musiclist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    musiclist: [],
    listInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'music',
      data: {
        playlistId: options.playlistId,
        $url: 'musiclist'
      }
    }).then((res) => {
      console.log(res)
      const pl = res.result.playlist
      this.setData({
        musiclist: pl.tracks,
        listInfo: {
          coverImgUrl: pl.coverImgUrl,
          name: pl.name,
        }
      })
      this._setMusiclist()
      wx.hideLoading()
    })
  },

  _setMusiclist() {
    wx.setStorageSync('musiclist', this.data.musiclist)
  },
})

After recompiling, into the song list page, open development tools Storage, view the cached data.

Here Insert Picture Description
We click on another song list, enter the list of songs, data in the cache has changed, it will overwrite the original description of the key corresponding to the content.
Here Insert Picture Description

Published 446 original articles · won praise 67 · views 240 000 +

Guess you like

Origin blog.csdn.net/hongxue8888/article/details/104626843