vue 分页 实现
Vue 分页实现方法
使用 Element UI 分页组件
Element UI 提供了现成的分页组件 el-pagination,适合快速集成。需要先安装 Element UI 库。
安装 Element UI:
npm install element-ui
在 Vue 文件中引入并使用:
<template>
<div>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 50]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="totalItems">
</el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
totalItems: 100
}
},
methods: {
handleSizeChange(val) {
this.pageSize = val;
this.fetchData();
},
handleCurrentChange(val) {
this.currentPage = val;
this.fetchData();
},
fetchData() {
// 模拟 API 请求
console.log(`Fetching page ${this.currentPage} with size ${this.pageSize}`);
}
}
}
</script>
自定义分页组件
如果需要完全自定义分页逻辑,可以手动实现分页功能。

模板部分:
<template>
<div class="pagination">
<button
@click="prevPage"
:disabled="currentPage === 1">
Previous
</button>
<span>Page {{ currentPage }} of {{ totalPages }}</span>
<button
@click="nextPage"
:disabled="currentPage === totalPages">
Next
</button>
</div>
</template>
脚本部分:

<script>
export default {
props: {
totalItems: {
type: Number,
required: true
},
itemsPerPage: {
type: Number,
default: 10
}
},
data() {
return {
currentPage: 1
}
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage);
}
},
methods: {
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
this.$emit('page-changed', this.currentPage);
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++;
this.$emit('page-changed', this.currentPage);
}
}
}
}
</script>
分页与表格数据绑定
分页通常需要与表格数据联动,以下是一个完整示例:
<template>
<div>
<table>
<tr v-for="item in paginatedData" :key="item.id">
<td>{{ item.name }}</td>
</tr>
</table>
<div class="pagination">
<button @click="prevPage">Previous</button>
<span>Page {{ currentPage }} of {{ totalPages }}</span>
<button @click="nextPage">Next</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
itemsPerPage: 5,
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
// ...更多数据
]
}
},
computed: {
totalPages() {
return Math.ceil(this.items.length / this.itemsPerPage);
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage;
const end = start + this.itemsPerPage;
return this.items.slice(start, end);
}
},
methods: {
prevPage() {
if (this.currentPage > 1) this.currentPage--;
},
nextPage() {
if (this.currentPage < this.totalPages) this.currentPage++;
}
}
}
</script>
分页与后端 API 集成
实际项目中,分页数据通常来自后端 API:
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
totalItems: 0,
items: []
}
},
mounted() {
this.fetchData();
},
methods: {
async fetchData() {
const response = await axios.get('/api/items', {
params: {
page: this.currentPage,
size: this.pageSize
}
});
this.items = response.data.items;
this.totalItems = response.data.total;
},
handlePageChange(page) {
this.currentPage = page;
this.fetchData();
}
}
}
</script>
样式优化
分页组件可以通过 CSS 美化:
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
cursor: pointer;
}
.pagination button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
以上方法涵盖了从简单到复杂的分页场景,可根据项目需求选择适合的实现方式。






