uniapp - load-more触底加载,下拉刷新

本文介绍如何使用uni-ui组件实现底部加载效果,包括more、loading和nomore三种状态,以及触底事件onReachBottom和下拉刷新onPullDownRefresh的处理。通过实例展示了如何在请求数据时更新状态并停止刷新。

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

底部加载load-more(uni-ui组件),三个状态:more、loading、nomore

触底事件:onReachBottom

下拉刷新:onPullDownRefresh,停止下拉刷新uni.stopPullDownRefresh()

<template>
  <view>
    <!-- 底部加载,三个状态:more、loading、nomore -->
    <uni-load-more :status="status"></uni-load-more>
  </view>
</template>

<script>
export default {
  data() {
    return {
      data: null,
      status: 'more', //触底加载状态
      page: 1, //记录当前页码
    };
  },
  //触底事件,请求数据、合并
  onReachBottom() {
    console.log('到底了到底了...');
    this.status = 'loading';
    this.getData(this.page + 1);
    this.page += 1;
  },
  //下拉刷新,请求第一页
  onPullDownRefresh() {
    this.getData();
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData(page = 1) {
      uni.request({
        url: 'https://xxxx.....',
        method: 'GET',
        data: { page: page },
        success: res => {
          console.log(res);
          //如果页数>1,需要拼接返回的数据
          if (page > 1) {
            res.data.result = [...this.data.result, ...res.data.result];
          }
          this.data = res.data;
          uni.stopPullDownRefresh(); //拿到数据后,停止下拉刷新
        },
        fail: () => {},
        complete: () => {}
      });
    },
  },
};
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值