uniapp中uni-load-more使用
1 引入uniloadmore
import uniloadmore from '@/components/uni-load-more/uni-load-more.vue'; components: {uniloadmore},
2 data中写的内容
reload: false, status: 'more', contenttext: { contentdown: '上拉加载更多~', contentrefresh: '加载中', contentnomore: '我是有底线的~' },
3 template里面写的内容
<uni-load-more :status="status" :icon-size="14" :content-text="contenttext" v-if="datalist.length > 0" />
4 请求接口成功之后,判断加载状态,处理数据
success: (result) => { this.totalcount = result.data.total if (result.data.total > 0) { const datamap = result.data.list this.datalist = this.reload ? datamap : this.datalist.concat(datamap); this.reload = false; } else { this.datalist = []; } if (this.totalcount == this.datalist.length) { this.reload = false; this.status = 'nomore' } }
5 监控加载状态
onreachbottom() { if (this.totalcount > this.datalist.length) { this.status = 'loading'; settimeout(() => { this.pagenum++ this.getmonthtask();//执行的方法 }, 1000)//这里我是延迟一秒在加载方法有个loading效果,如果接口请求慢的话可以去掉 } else { //停止加载 this.status = 'nomore' } },
uniapp - load-more触底加载,下拉刷新
底部加载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>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论