uniapp上拉加载 加载更多 uni-load-more组件使用

1、给渲染的数据加上滚动加载 和 uni-load-more 组件

<scroll-view class="list" scroll-y="true">

</scroll-view>

<uni-load-more :status="status" :content-text="contentText" />

2、引入组件 和 数据初始化

import uniLoadMore from "@/uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue"
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				searchValue: '',
				universityList: [],
				page: 1,
				limit: 10,
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			}
		},

3、加上下拉刷新和上拉加载

下拉刷新的时候将 页码重置为1

上拉加载的时候 如果没有更多则不加载数据 否则 page ++ 然后获取第二页的数据

        //下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			uni.stopPullDownRefresh();
			this.universityList = [];
			this.getUniversityList()
		},
		//上拉加载
		onReachBottom() {
			if (this.status == 'noMore') {
				return;
			}
			this.page++;
			this.getUniversityList();
		},

4、接收数据 如果页码为1 则直接赋值 否则 加上之前的数据

判断数据的长度是否小于 我们需要的长度 如果小于 那就是没有更多数据 

                    if (this.page === 1) {
						this.universityList = res.data;
					} else {
						this.universityList = this.universityList.concat(res.data);
					}
					// 判断是否还有更多数据
					if (res.data.length < this.limit) {
						this.status = 'noMore'; // 没有更多数据
					} else {
						this.status = 'more'; // 还有更多数据
					}

完整代码

<template>

	<view class="main">

		<view class="top-content">
			<view class="search">
				<view class="ipt">
					<image class="searchPic" src="../../static/img/searchpic.png"></image>
					<input class="searchIpt" v-model="searchValue" type="text" placeholder="请输入搜索内容"
						placeholder-style="color:#E5E5E5;font-size:24rpx;font-weight:400;">
				</view>
				<view class="btn">
					<button class="searchBtn" @click="onSearch">搜索</button>
				</view>
			</view>
		</view>

		<view class="content">
			<scroll-view class="list" scroll-y="true">
				<view class="university" v-for="(item,index) in universityList" :key="index" @click="go(item.id)">
					<view class="universityNav">
						<view class="universityInfo">{{item.code}} {{item.name}}</view>
						<view>
							<image class="go" src="../../static/img/in.png"></image>
						</view>
					</view>
				</view>
			</scroll-view>
			<uni-load-more :status="status" :content-text="contentText" />
		</view>

	</view>

</template>

<script>
	import uniLoadMore from "@/uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue"
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				searchValue: '',
				universityList: [],
				page: 1,
				limit: 10,
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			}
		},
		onLoad() {
			this.getUniversityList()
		},
		//下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			uni.stopPullDownRefresh();
			this.universityList = [];
			this.getUniversityList()
		},
		//上拉加载
		onReachBottom() {
			if (this.status == 'noMore') {
				return;
			}
			this.page++;
			this.getUniversityList();
		},
		methods: {
			async getUniversityList() {
				let res = await this.$myHttp.post({
					url: this.$myHttp.urlMap.getVideoUniversityList,
					data: {
						page: this.page,
						limit: this.limit,
						name: this.searchValue
					},
					needLogin: true
				})
				if (res.code == 1) {
					if (this.page === 1) {
						this.universityList = res.data;
					} else {
						this.universityList = this.universityList.concat(res.data);
					}
					// 判断是否还有更多数据
					if (res.data.length < this.limit) {
						this.status = 'noMore'; // 没有更多数据
					} else {
						this.status = 'more'; // 还有更多数据
					}
				}
			},
			onInput(event) {
				console.log(event)
				this.searchValue = event.target.value;
			},
			// 监听搜索框输入事件
			onSearch() {
				this.page = 1; // 重新搜索从第一页开始
				this.universityList = [];
				this.getUniversityList();
			},
			go(id) {
				uni.navigateTo({
					url: '/pages/video/video?id=' + JSON.stringify(id)
				});
			}
		}
	}
</script>

   

原创链接 在基础上做修改

uni-app 上拉加载 使用uni-ui 的 LoadMore 组件_uni-load-more 上拉没反应_O3ohn的博客-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/ka_xingl/article/details/110083976

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
UniApp中,可以使用uni-scroll-view组件实现上拉加载和下拉刷新的功能。 首先,在页面中引入uni-scroll-view组件: ```vue <template> <uni-scroll-view class="scroll-view" :enable-back-to-top="true" :scroll-y="true" :lower-threshold="20" :upper-threshold="20" :refresher-enabled="true" :refresher-threshold="45" :refresher-default-style="true" @scrolltolower="loadMoreData" @refresherrefresh="refreshData"> <!-- 内容区域 --> </uni-scroll-view> </template> ``` 其中,`:scroll-y="true"`表示支持纵向滚动,`:lower-threshold="20"`表示距离底部多少距离时触发上拉加载,`:upper-threshold="20"`表示距离顶部多少距离时触发下拉刷新,`:refresher-enabled="true"`表示启用下拉刷新功能,`:refresher-threshold="45"`表示下拉刷新的临界值,`:refresher-default-style="true"`表示使用默认的下拉刷新样式。 在`<uni-scroll-view>`标签中的内容区域,可以放置展示数据的列表或其他组件。 然后,在页面的script中,编写下拉刷新和上拉加载的方法: ```vue <script> export default { data() { return { dataList: [], // 数据列表 page: 1, // 当前页码 pageSize: 10, // 每页条数 isLoadMore: false, // 是否正在加载更多数据 isRefresh: false // 是否正在下拉刷新 } }, methods: { // 加载更多数据 loadMoreData() { if (this.isLoadMore) { return } this.isLoadMore = true this.page++ // 调用加载数据的接口获取数据 // 将获取到的数据添加到dataList中 // 加载完成后,将isLoadMore设为false }, // 下拉刷新数据 refreshData() { if (this.isRefresh) { return } this.isRefresh = true this.page = 1 // 调用加载数据的接口获取数据 // 将获取到的数据替换dataList中的数据 // 刷新完成后,将isRefresh设为false } } } </script> ``` 在loadMoreData方法中,首先判断是否正在加载更多数据,如果是,则不再执行加载数据的操作。然后将isLoadMore设为true,表示正在加载更多数据。接着,调用加载数据的接口获取数据,并将获取到的数据添加到dataList中。最后,将isLoadMore设为false,表示加载完成。 在refreshData方法中,同样需要判断是否正在下拉刷新。然后将isRefresh设为true,表示正在刷新数据。接着,将页码设为1,表示刷新第一页数据。调用加载数据的接口获取数据,并将获取到的数据替换dataList中的数据。最后,将isRefresh设为false,表示刷新完成。 这样就实现了基于Vue3的UniApp上拉加载和下拉刷新功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值