uniapp项目中监听用户的下拉刷新事件

在pages.json中配置启用下拉刷新,并设置页面样式。在onPullDownRefresh事件中重置数据并调用获取数据的方法,完成后使用uni.stopPullDownRefresh关闭下拉刷新。getGoodsList方法异步获取数据并执行回调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在 pages.json 配置文件中找到需要监听用户下拉刷新的页面修改配置如下:

{
   "path" : "goods_list/goods_list",
   "style" :                                                                                    
  {
     "navigationBarTitleText": "",

     // 开启下拉刷新
     "enablePullDownRefresh": true,
    
     // 页面背景色   就是下拉的那个颜色
     "backgroundColor": "#f8f8f8", 
    
     "onReachBottomDistance": 150
  }

}

2. 页面通过 onPullDownRefresh 回调监听用户下拉刷新的动作,并做相应的处理如下:

    onPullDownRefresh() {

// 页码重置为  第一页
      this.queryObj.pagenum = 1

// 总数据 重置为 0
      this.total = 0

// 关闭节流阀
      this.isLoading = false

// 数据重置为空数组
      this.goodsList = []
      
// 重点:重新调用获取数据的函数 并且传一个 回调 uni.stopPullDownRefresh() 用来关闭下拉刷新
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }

3. 在 获取数据的函数中 通过 cb 接收回调并使用

    async getGoodsList(cb){
        this.isLoading = true
        const {message:{total, goods}} = await api.getData("/goods/search", this.queryObj)
        // console.log(message)
        this.isLoading = false

        // 随便定义一个字段接收    通过短路运算符 && 判断有的话就执行            
        cb && cb()
        
        this.goodsList = [...this.goodsList, ...goods]
        this.total = total
      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值