vue分页组件的实现
基础分页组件实现
创建一个基本的Vue分页组件需要定义当前页码、总页数等核心属性,并通过事件触发页码切换。
模板部分
<template>
<div class="pagination">
<button
@click="changePage(currentPage - 1)"
:disabled="currentPage <= 1"
>
上一页
</button>
<span v-for="page in pages" :key="page">
<button
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</button>
</span>
<button
@click="changePage(currentPage + 1)"
:disabled="currentPage >= totalPages"
>
下一页
</button>
</div>
</template>
脚本部分
<script>
export default {
props: {
totalItems: {
type: Number,
required: true
},
itemsPerPage: {
type: Number,
default: 10
},
currentPage: {
type: Number,
default: 1
}
},
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>
高级分页功能
对于数据量大的情况,需要优化分页显示逻辑,避免显示过多页码按钮。
计算属性优化
computed: {
pages() {
const current = this.currentPage
const total = this.totalPages
const range = 2 // 显示当前页前后各2页
if (total <= 5) {
return Array.from({ length: total }, (_, i) => i + 1)
}
let pages = []
pages.push(1)
if (current - range > 2) {
pages.push('...')
}
const start = Math.max(2, current - range)
const end = Math.min(total - 1, current + range)
for (let i = start; i <= end; i++) {
pages.push(i)
}
if (current + range < total - 1) {
pages.push('...')
}
pages.push(total)
return pages
}
}
与表格数据结合
分页组件通常需要与表格数据配合使用,下面展示如何在实际场景中集成。

父组件使用示例
<template>
<div>
<table>
<!-- 表格内容 -->
</table>
<pagination
:total-items="totalItems"
:items-per-page="perPage"
:current-page="currentPage"
@page-changed="handlePageChange"
/>
</div>
</template>
<script>
import Pagination from './Pagination.vue'
export default {
components: { Pagination },
data() {
return {
currentPage: 1,
perPage: 10,
totalItems: 0,
items: []
}
},
methods: {
async fetchData(page = 1) {
const response = await api.getItems({
page,
limit: this.perPage
})
this.items = response.data
this.totalItems = response.total
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
},
created() {
this.fetchData()
}
}
</script>
样式优化
为分页组件添加基础样式提升用户体验。
CSS样式

<style scoped>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
border: 1px solid #ddd;
background: white;
cursor: pointer;
}
.pagination button:hover:not(:disabled) {
background: #eee;
}
.pagination button.active {
background: #42b983;
color: white;
border-color: #42b983;
}
.pagination button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
服务器端分页处理
当数据量极大时,需要实现服务器端分页以减少前端压力。
API请求示例
async fetchData(page = 1) {
try {
const response = await axios.get('/api/items', {
params: {
page,
limit: this.perPage
}
})
this.items = response.data.items
this.totalItems = response.data.totalCount
} catch (error) {
console.error('获取数据失败:', error)
}
}
性能优化技巧
对于大型数据集,可以采用虚拟滚动或懒加载等技术提升性能。
虚拟滚动实现思路
// 使用vue-virtual-scroller等库
<template>
<RecycleScroller
:items="visibleItems"
:item-size="50"
key-field="id"
>
<template v-slot="{ item }">
<!-- 渲染单条数据 -->
</template>
</RecycleScroller>
<pagination
:total-items="totalItems"
@page-changed="handlePageChange"
/>
</template>






