fix page list for RepositoryList.vue

This commit is contained in:
Timi
2025-07-15 16:53:31 +08:00
parent 70111ce18b
commit cef8121008

View File

@ -22,13 +22,16 @@
</t-list> </t-list>
<footer class="footer"> <footer class="footer">
<t-pagination <t-pagination
class="pagination" class="pages"
v-if="pageResult"
:total="pageResult.total" :total="pageResult.total"
:pageSize="16" :pageSize="16"
:showPageSize="false" :showPageSize="false"
:current="currentPage"
:onCurrentChange="onPageChangeEvent"
> >
<template #totalContent> <template #totalContent>
<div class="total" v-text="`共 ${pageResult.total} 个项目`"></div> <div style="flex: 1" v-text="`共 ${pageResult.total} 篇文章`"></div>
</template> </template>
</t-pagination> </t-pagination>
</footer> </footer>
@ -37,24 +40,62 @@
<script lang="ts" setup> <script lang="ts" setup>
import RepositoryAPI from "@/api/RepositoryAPI"; import RepositoryAPI from "@/api/RepositoryAPI";
import { Repository } from "@/types/Repository"; import { Repository } from "@/types/Repository";
import { Icon, Loading, Page, PageResult, Time } from "timi-web"; import { Icon, Loading, Page, PageResult, Time, Toolkit } from "timi-web";
const page = ref<Page>({ const router = useRouter();
const route = useRoute();
const isLoading = ref(false);
const currentPage = computed(() => page.index + 1);
const page = reactive<Page>({
index: 0, index: 0,
size: 12 size: 16
}); });
const pageResult = reactive<PageResult<Repository>>({ const pageResult = reactive<PageResult<Repository>>({
total: 0, total: 0,
list: [] list: []
}); });
const isLoading = ref(false); // 获取列表
onMounted(async () => { const fetchList = Toolkit.debounce(async () => {
isLoading.value = true; isLoading.value = true;
const result = await RepositoryAPI.page(page.value); const result = await RepositoryAPI.page(page);
pageResult.total = result.total; pageResult.total = result.total;
pageResult.list = result.list; pageResult.list = result.list;
isLoading.value = false; isLoading.value = false;
}); });
// 监听路由分页
watch(
() => route.query.p,
async (newP) => {
const pNum = Number(newP);
if (Number.isInteger(pNum) && 0 <= pNum) {
page.index = pNum;
await fetchList();
} else {
// 参数无效时重置为第一页
await router.push({
path: route.path,
query: {
...route.query,
p: 0
}
});
}
},
{ immediate: true }
);
function onPageChangeEvent(current: number) {
const newIndex = current - 1;
router.push({
path: route.path,
query: {
...route.query,
p: newIndex
}
});
}
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>