uniapp的下拉刷新和上拉加载

首先来说uniapp本身有对应的上拉加载和下拉刷新对应的生命周期

现在对自己一直写着的上拉加载和下拉刷新做个总结,优化


<template>
	<view class="appointment" v-if="yuershiSubscribeList.length > 0">
		<view class="list" v-for="(item,index) in yuershiSubscribeList" :key="item.id">
			<view class="detail">
				<text class="name">{{item.name}}</text>
				<text class="type">类型 : <text class="color">{{item.mom_name}}</text></text>
				<text class="time">预约时间 : {{parseInt(item.time) | c_time('yyyy-MM-dd hh:mm:ss')}}</text>
				<text class="distance">地址 : {{item.address}} </text>
			</view>
			<image class="head" :src="item.image" mode=""></image>
		</view>
	</view>
</template>

<script>
	import teacherModelClass from '@/common/model/teacher.js';
	
	const teacherModel = new teacherModelClass();
	export default {
		data() {
			return {
				loadingText: '加载中...',
				loadingType: 0,
				contentText: {
					contentdown:'上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
				page:1,
				row: 10,
				yuershiSubscribeList:[],
			}
		},
		onReachBottom(){
			var  _this = this
			this.getmoreinfo();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.loadingType = 0;
			this.get_data();
			uni.stopPullDownRefresh();
		},
		onLoad() {
			this.get_data();
		},
		methods: {
			get_data(){
				let _this = this
				teacherModel.yuershiSubscribe(_this.page,_this.row).then(res => {
					console.log(res);
					if(res.code === 2){return false}
					_this.yuershiSubscribeList = res;
					console.log(JSON.stringify(this.yuershiSubscribeList))
					 
				});
			},
			// 加载更多
			getmoreinfo() {
				let _this = this;
				if (_this.loadingType !== 0) {//loadingType!=0;直接返回
					return false;
				}
				_this.loadingType = 1;
				_this.page ++;
				var params={
					page:_this.page,
					row: _this.row
				};
				uni.showNavigationBarLoading();//显示加载动画
				_this.post('v1/Yuershi/my_yuershi_subscribe',params,(data)=>{
					console.log(data);
					uni.hideNavigationBarLoading();//关闭加载动画
					if(data.code == 2){
						uni.showToast({
							title:'没有更多啦...',
							icon:'none',
							duration:2000
						})
						return false;
					}else{
						if (data.data == null) {//没有数据
							_this.loadingType = 2;
							uni.hideNavigationBarLoading();//关闭加载动画
							return;
						}
						var new_info = data.data;
						_this.yuershiSubscribeList = _this.yuershiSubscribeList.concat(new_info);//将数据拼接在一起
						_this.loadingType = 0;//将loadingType归0重置
						uni.hideNavigationBarLoading();//关闭加载动画	
					}
				});//更多会员商品
			},
		}
	}
</script>

下面是效果下拉刷新效果
在这里插入图片描述
下面是上拉加载效果:
在这里插入图片描述
这是防止重复刷新或者加载的代码(上锁)

if (_this.loadingType !== 0) {//loadingType!=0;直接返回
					return false;
				}
_this.loadingType = 1;

这是定义相关加载文字的data

data() {
			return {
				loadingText: '加载中...', // 加载效果动画
				loadingType: 0, // 上锁
				contentText: {
					contentdown:'上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
				page:1,
				row: 10,
				yuershiSubscribeList:[],
			}
		},

这是对应的下拉刷新和上拉加载的生命周期(钩子),在这里调用刷新加载事件

onReachBottom(){
// 这是上拉加载对应的生命周期 调用 getmoreinfo()方法,该方法是对上拉加载的一些优化
			var  _this = this
			this.getmoreinfo();
		},
onPullDownRefresh() {
// 这是上拉刷新的生命周期(钩子)
			this.page = 1;
			this.loadingType = 0;
			this.get_data();
			uni.stopPullDownRefresh(); // 这是停止上拉刷新的事件
		},
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值