底部加载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>