微信小程序之上拉加载 下拉刷新

首先需要在app.json里开启

"enablePullDownRefresh": true,

然后

onPullDownRefresh 监听用户下拉刷新

wx.showNavigationBarLoading();  显示刷新图标

wx.stopPullDownRefresh();  停止下拉动作

这是我写的下拉刷新

onPullDownRefresh:function(){
      page  = 1;
      isclose = false;
      this.setData({student:[],contition:""})
      var that = this;
      refresh(that)
    },

调用的函数

var page = 1;
var isclose = false;
function refresh(that){
  if(isclose){return}
  wx.showLoading({
    title: '加载中',
  })
  wx.request({
    url: 'xxxx', //仅为示例,并非真实的接口地址
    data: {
      page:page,
      contition:that.data.contition
    },
    header: {
      'content-type': 'application/json', // 默认值
      "Cookie":wx.getStorageSync('session_id'),
    },
    success: (res) => {
      wx.hideLoading();
      if(res.data.length>0){
        var student = that.data.student;
        for (var i = 0; i < res.data.length;i++){
          student.push(res.data[i])
        }
        that.setData({ student: student })
        page++;
      }else{
        isclose = true;
      }
    }
  })
}

上拉加载

onReachBottom:function(){
        var that = this;
        refresh(that)
    }



猜你喜欢

转载自blog.csdn.net/woshigemengxin/article/details/80492472