From a25a4272884f952f7cb2501c68761d0cebacc651 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 29 Jun 2023 22:00:43 +0800 Subject: [PATCH] add VInfiniteScroll --- src/views/discover/MediaCardListView.vue | 43 ++++++++++++++++++------ 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/views/discover/MediaCardListView.vue b/src/views/discover/MediaCardListView.vue index 09b583a9..8acd5ab5 100644 --- a/src/views/discover/MediaCardListView.vue +++ b/src/views/discover/MediaCardListView.vue @@ -8,6 +8,11 @@ const props = defineProps({ apipath: String, }); +// 当前页码 +const page = ref(1); +// 是否加载中 +const loading = ref(false); + // 数据列表 const dataList = ref([]); @@ -17,22 +22,40 @@ const fetchData = async () => { if (!props.apipath){ return } - dataList.value = await api.get(props.apipath); + // 如果正在加载中,直接返回 + if (loading.value) { + return; + } + // 设置加载中 + loading.value = true; + const data = await api.get(props.apipath, { + params: { + page: page.value, + }, + }); + // 合并数据 + dataList.value = [...dataList.value, ...data]; + // 页码+1 + page.value++; + // 取消加载中 + loading.value = false; } catch (error) { console.error(error); } }; -// 加载时获取数据 -onMounted(fetchData); -