uni-app 滚动加载
封装继承组件
创建文件/components/extends/PaginationList.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| <template> <!-- 报错提示框 --> <u-modal v-model="showErrorTip" :mask-close-able="true" :content="tipContent"></u-modal> </template>
<script>
export default { name: "PaginationList", data() { return { service: null, listData: [], totalCount: null, loadText: { loadmore: '上拉显示更多', loading: '正在加载...', nomore: '没有更多数据了' }, status: 'nomore',
urlParam: {}, isLoading: false, showErrorTip: false, tipContent: '', } }, onLoad() { this.urlParam = { page: 0, size: 10 } }, methods: { loadData(page) { this.initExtraData(); uni.showLoading({mask: true}); this.isLoading = true; if(page){ this.urlParam= Object.assign(this.urlParam, page) }else{ this.urlParam= Object.assign(this.urlParam, {page:0}) } this.service.query(this.urlParam).then((res) => { this.totalCount = Number(res.header['x-total-count']); this.totalCount = Number(res.header['X-Total-Count']); if (res.statusCode === 200 && res.data && res.data.length > 0) { if(this.urlParam.page===0){ this.listData = [] } if (this.listData.length === 0) { this.listData = res.data.map(item => Object.assign({}, item)) } else { this.listData = [...this.listData, ...res.data] } if (this.totalCount === this.listData.length) { this.status = 'nomore'; } else { this.status = 'loadmore'; } uni.hideLoading(); this.isLoading = false } else { this.$msg('未查询到数据'); this.listData = []; uni.hideLoading(); this.isLoading = false } }).catch(() => { this.$msg('数据请求失败'); this.listData = []; uni.hideLoading(); this.isLoading = false }) }, loadmore() { if (this.totalCount === this.listData.length) { this.status = 'nomore'; } else { this.urlParam.page = this.urlParam.page+1; this.status = 'loading'; this.loadData({page:this.urlParam.page}); } }, initExtraData() {} }
} </script>
<style></style>
|
组件应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <template> <view class="wrapper"> <scroll-view scroll-y="true" class="container" @scrolltolower="loadmore"> </scroll-view> </view> </template>
<script> import PaginationList from '@/components/extends/PaginationList.vue' import testService from "@/api/service/testService.js" export default { extends: PaginationList, data() { service: testService }, methods: { filterData() { this.urlParam = { page: 0, size: 999 } this.loadData() } } } </script>
<style lang="scss" scoped> .wrapper { display: flex; flex-direction: column; height: 100%; .container { flex: 1; overflow: scroll; } } </style>
|