Feasibility and examples of WeChat applet cloud development using wx.redirectTo to realize page refresh function

Not much to say, enter the text, as shown below:

When developing a simple diary applet, in order to break through the limitation of loading only 20 entries per query of the cloud development database, the following code was used:

 //获取列表数据
  getTask(){
    //数据加载中的友好提示
    wx.showLoading({
      title: '数据加载中',
    })    
    console.log("当前list的长度:",this.data.list.length);
    let len = this.data.list.length           //当前list的长度赋值给变量len
    wx.cloud.database().collection("diary")
    .orderBy('create_time', 'desc')
    .skip(len)    //skip跳过len长度
    .get()
    .then(res=>{
      //数据加载成功,隐藏加载提示
      wx.hideLoading()
      console.log("查询成功",res);
      //数据加载完成的友好提示
      let dataList = res.data
      if(dataList.length<=0){
        wx.showToast({
          title: '数据加载完成',
          icon:"none"
        })
      }
      this.setData({
        //保留前面加载过的数据,使用concat连接
        list:this.data.list.concat(res.data)
      })
    })
    .catch(err=>{
      //数据加载失败,隐藏加载提示
      wx.hideLoading()
      console.log("查询失败",err);
    })
  },

 But when you want to implement pull-down refresh, you can't execute this code repeatedly, or you need to pass a new value to execute this code repeatedly, otherwise you can't perform the refresh operation, and you will only achieve the bottom-out loading effect.

Since it is not a way to display the page unloading and page loading, it is decided to use the original API of WeChat to achieve the effect of reloading the page after unloading instead of refreshing. The code is as follows:

onPullDownRefresh: function () {
wx.redirectTo({
  url: '../diary/diary',
})
},

The effect is as follows:

 Summary: For small programs with small page data, you can choose this method to realize automatic page refresh and pull-down refresh. So far, no bugs caused by this method have been found. If there is a better method, please let me know.

Guess you like

Origin blog.csdn.net/sdqmrj/article/details/126458802