用vue实现分页
使用 Vue 实现分页
分页组件的基本结构
创建一个独立的 Pagination.vue 组件,包含页码按钮、上一页和下一页按钮。通过 props 接收总页数、当前页等参数,通过 emit 触发页码变更事件。

<template>
<div class="pagination">
<button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">
上一页
</button>
<button
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: page === currentPage }"
>
{{ page }}
</button>
<button
@click="changePage(currentPage + 1)"
:disabled="currentPage === totalPages"
>
下一页
</button>
</div>
</template>
<script>
export default {
props: {
totalItems: Number,
itemsPerPage: Number,
currentPage: Number,
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage);
},
pages() {
const range = [];
for (let i = 1; i <= this.totalPages; i++) {
range.push(i);
}
return range;
},
},
methods: {
changePage(page) {
if (page >= 1 && page <= this.totalPages) {
this.$emit("page-changed", page);
}
},
},
};
</script>
<style>
.pagination button {
margin: 0 5px;
padding: 5px 10px;
cursor: pointer;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
.pagination button:disabled {
cursor: not-allowed;
opacity: 0.5;
}
</style>
在父组件中使用分页
在父组件中引入分页组件,传递总数据量、每页条数和当前页码,监听页码变化事件并更新数据。

<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<Pagination
:total-items="data.length"
:items-per-page="itemsPerPage"
:current-page="currentPage"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from "./Pagination.vue";
export default {
components: { Pagination },
data() {
return {
data: [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
// 更多数据...
],
itemsPerPage: 5,
currentPage: 1,
};
},
computed: {
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage;
const end = start + this.itemsPerPage;
return this.data.slice(start, end);
},
},
methods: {
handlePageChange(page) {
this.currentPage = page;
},
},
};
</script>
优化分页显示
对于大量页码的情况,可以只显示当前页附近的页码,避免按钮过多。修改 pages 计算属性:
pages() {
const range = [];
const maxVisible = 5; // 最多显示的页码数
let start = Math.max(1, this.currentPage - Math.floor(maxVisible / 2));
let end = Math.min(this.totalPages, start + maxVisible - 1);
if (end - start + 1 < maxVisible) {
start = Math.max(1, end - maxVisible + 1);
}
for (let i = start; i <= end; i++) {
range.push(i);
}
return range;
},
添加省略号效果
进一步优化显示效果,在首尾页码不连续时显示省略号:
<template>
<div class="pagination">
<button @click="changePage(1)" :disabled="currentPage === 1">首页</button>
<button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">
上一页
</button>
<button v-if="showFirstEllipsis" disabled>...</button>
<button
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: page === currentPage }"
>
{{ page }}
</button>
<button v-if="showLastEllipsis" disabled>...</button>
<button
@click="changePage(currentPage + 1)"
:disabled="currentPage === totalPages"
>
下一页
</button>
<button @click="changePage(totalPages)" :disabled="currentPage === totalPages">
尾页
</button>
</div>
</template>
<script>
export default {
computed: {
showFirstEllipsis() {
return this.pages[0] > 1;
},
showLastEllipsis() {
return this.pages[this.pages.length - 1] < this.totalPages;
},
},
};
</script>
通过这些方法可以实现一个功能完善且用户友好的分页组件,适用于大多数 Vue 项目需求。






