uniapp和uview组件实现下拉触底刷新列表

该文演示了如何在UniApp项目中结合uViewUI组件库和Axios库实现下拉触底加载更多功能。通过在Vue页面中引入uView,设置相关数据状态,在生命周期钩子中请求分页数据,并利用uView的LoadMore组件展示加载状态。
摘要由CSDN通过智能技术生成

下面是一个在UniApp中使用uView组件实现下拉触底刷新列表的示例,并使用Axios来请求分页数据列表:

  1. 首先,确保你已经在UniApp项目中添加了uView组件库。你可以在项目根目录执行以下命令安装它们:
npm install uview-ui

或者使用
Hbuilder X 的插件市场进行引入使用,详情请看官网(建议使用)

https://v1.uviewui.com/components/install.html

  1. 在你的Vue页面中,引入uView组件库和Axios库:
import uView from 'uview-ui';
Vue.use(uView);

//或者

import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
  1. 在你的页面data中定义相关的变量:
data() {
  return {
    list: [], // 存储列表数据
    page: 1, // 当前页数
    pageSize: 10, // 每页数据数量
    total: 0, // 总数据条数
    isLoading: false, // 是否正在加载数据
    status: 'loadmore',
	iconType: 'flower',
	loadText: {
		loadmore: '轻轻上拉',
		loading: '努力加载中',
		nomore: '实在没有了'
	},
  }
},
  1. 在mounted生命周期中初始化页面数据,例如在页面加载时请求第一页的数据:
onShow() {
  this.loadData();
},
  1. 编写请求数据的方法,使用uView插件自带的请求方式发送分页数据请求:
methods: {
  loadData() {
    this.$u.get('url', {
        page: this.page,
        pageSize: this.pageSize
    })
    .then(response => {
      const data = response.data;
      this.list = this.list.concat(data.list); // 将返回的数据添加到列表数据中
      this.total = data.total; // 更新总数据条数
    })
    .catch(error => {
      console.error(error);
    });
  }
},
  1. 在template中使用uView的LoadMore组件来实现下拉触底刷新列表的效果:
<template>
  <view>
      <ul v-for="(item, index) in list" :key="index">
        <li>{{ item.title }}</li>
      </ul>
    <u-loadmore :status="status" :icon-type="iconType" :load-text="loadText" />
  </view>
</template>

具体实例可以看uView官网:https://v1.uviewui.com/components/loadMore.html

7、在生命周期添加下面内容

onReachBottom() {
			let that = this;
			if(this.tableData.length == this.total) return ;
			this.status = 'loading';
			this.page = ++ this.page;
			setTimeout(() => {
				that.loadData(); //请求的接口数据
				if(this.tableData.length == this.total) this.status = 'nomore';
				else this.status = 'loading';
			}, 2000)
		},

在这里插入图片描述

在上述例子中:

  • loadData()方法用于发送请求获取分页数据,并更新页面的数据。
  • list变量用来存储列表数据。
  • status 变量用来标识数据是否正在加载的文字。
  • onReachBottom用于触发获取下一页数据。

以上示例使用了uView的LoadMore组件来实现下拉触底刷新列表,你可以根据自己的需求进行修改和调整,如改变请求的接口地址和参数,修改列表的渲染方式等。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一介青烟小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值