0
点赞
收藏
分享

微信扫一扫

上拉刷新 ,下拉加载( + 防止多次请求)

上拉刷新

  • 配置样式 ( 全局 / 局部): .json文件中,
  1.  下拉刷新 窗口背景色 ----- 仅支持16进制(即:#eeeeee,不支持red,pink等英文)
  2. 下拉刷新loading样式 ----- 仅支持 'dark'、'light' 

    {
      "usingComponents": {},
      "enablePullDownRefresh": true,//本页面开启下拉刷新
      "backgroundColor": "#349edf",//背景色,目前仅支持16进制,如:#eeeeee
      "backgroundTextStyle": "dark"//loadingicon颜色,目前仅支持“dark”、“light”
    }
  3.  onPullDownRefresh方法中,手动添加停止方法

    /**
     * 页面相关事件处理函数--监听用户下拉动作 
     */
    // 上拉刷新
    onPullDownRefresh: function () {
        console.log('refresh');
        // 上拉刷新 loading不会主动消失,手动停止
        wx.stopPullDownRefresh({
            success: (res) => {},
        })
    },

     

下拉加载:(添加节流,防止未请求回数据时,会多次请求)

  1. 定义一个“节流阀” : isLoading : false
  2. 请求数据前开启“节流阀”:isLoading : true
  3. 请求数据完成后,关闭“节流阀”:isLoading : false ----- wx.request 方法中的complete函数中关闭
  4. 触底时判断是否上次数据已返回,可继续请求:onReachBottom方法中(触底)添加isLoading的判断
    data: {
        colorList: [],
        isLoading: false, //节流阀:判断是否已经请求完成,并返回数据。如果已返回则关闭节流阀,可再次请求下次数据
    },
    // 获取tablelist颜色
    getColorlists() {
        // 发起请求前:修改 节流阀 的值(开启)
        this.setData({
            isLoading: true
        })
        // 添加 数据加载中loading 
        wx.showLoading({
            title: '加载中',
        })
        wx.request({
            url: 'https://www.XXXX.cn/api/color',
            type: 'Get',
            success: (res) => {
                this.setData({
                    // 新数据与已请求回来的数据合并
                    colorList: [...this.data.colorList, ...res.data]
                })
            },
            // 无论请求 成功/失败 后的响应
            complete: () => {
                // 关闭 数据加载中的loading
                wx.hideLoading()
                // 完成请求后:修改 节流阀 的值(关闭)
                this.setData({
                    isLoading: false
                })
            }
        })
    },
    onReachBottom: function () {
        // 判断是否节流阀在开启状态中( 开启:还在请求数据的路上,数据未加载请求回来)
        if (!this.data.isLoading) {
            console.log('reach 打印查看是否多次出发')
            this.getColorlists()
        }
    },

举报

相关推荐

0 条评论