vue组件实现分页复用
分页组件封装思路
封装一个可复用的Vue分页组件需要考虑数据独立性、配置灵活性和事件通信。以下是一个典型实现方案:
基础分页组件实现
创建Pagination.vue组件文件:

<template>
<div class="pagination">
<button
:disabled="currentPage <= 1"
@click="changePage(currentPage - 1)"
>上一页</button>
<span v-for="page in pageRange" :key="page">
<button
:class="{ active: currentPage === page }"
@click="changePage(page)"
>{{ page }}</button>
</span>
<button
:disabled="currentPage >= totalPages"
@click="changePage(currentPage + 1)"
>下一页</button>
</div>
</template>
<script>
export default {
props: {
totalItems: {
type: Number,
required: true
},
itemsPerPage: {
type: Number,
default: 10
},
currentPage: {
type: Number,
default: 1
},
maxVisibleButtons: {
type: Number,
default: 5
}
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage)
},
pageRange() {
const range = []
const half = Math.floor(this.maxVisibleButtons / 2)
let start = Math.max(this.currentPage - half, 1)
let end = Math.min(start + this.maxVisibleButtons - 1, this.totalPages)
if (end - start + 1 < this.maxVisibleButtons) {
start = Math.max(end - this.maxVisibleButtons + 1, 1)
}
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
},
methods: {
changePage(page) {
if (page >= 1 && page <= this.totalPages) {
this.$emit('page-changed', page)
}
}
}
}
</script>
<style scoped>
.pagination {
display: flex;
gap: 5px;
}
.active {
background-color: #42b983;
color: white;
}
</style>
父组件调用方式
在需要使用分页的父组件中:
<template>
<div>
<!-- 数据列表展示 -->
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<!-- 分页组件 -->
<Pagination
:total-items="dataList.length"
:items-per-page="perPage"
:current-page="currentPage"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: { Pagination },
data() {
return {
dataList: [], // 从API获取的全部数据
perPage: 10,
currentPage: 1
}
},
computed: {
paginatedData() {
const start = (this.currentPage - 1) * this.perPage
const end = start + this.perPage
return this.dataList.slice(start, end)
}
},
methods: {
handlePageChange(page) {
this.currentPage = page
// 如果需要可以在这里触发API请求
}
}
}
</script>
高级功能扩展
- 每页条数选择器
<select v-model="itemsPerPage" @change="handlePerPageChange"> <option value="5">5条/页</option> <option value="10">10条/页</option> <option value="20">20条/页</option> </select>
methods: { handlePerPageChange() { this.currentPage = 1 this.$emit('per-page-changed', this.itemsPerPage) } }

2. 跳转到指定页码
```vue
<input
type="number"
:min="1"
:max="totalPages"
v-model.number="jumpPage"
@keyup.enter="changePage(jumpPage)"
>
- 总条数显示
<div class="total-count">共 {{ totalItems }} 条记录</div>
服务器分页适配
对于后端分页的场景,修改分页逻辑:
methods: {
async handlePageChange(page) {
this.currentPage = page
const response = await api.fetchData({
page,
size: this.itemsPerPage
})
this.paginatedData = response.data
this.totalItems = response.total
}
}
样式自定义方案
通过插槽和CSS变量增强自定义能力:
<template>
<div class="pagination" :style="cssVars">
<slot name="prev" :page="currentPage" :disabled="currentPage <= 1">
<button>上一页</button>
</slot>
<!-- ... -->
</div>
</template>
<script>
props: {
themeColor: {
type: String,
default: '#42b983'
}
},
computed: {
cssVars() {
return {
'--theme-color': this.themeColor
}
}
}
</script>
<style scoped>
.pagination button.active {
background-color: var(--theme-color);
}
</style>
这种实现方式提供了高度可复用的分页组件,支持客户端分页和服务器分页两种模式,同时允许深度自定义样式和行为。






