vue 分页 实现
分页组件实现
在Vue中实现分页功能通常需要结合后端API和前端组件。以下是两种常见的实现方式:
基于Element UI的分页
Element UI提供了现成的分页组件,可以快速集成:
<template>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 50, 100]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
total: 0,
list: []
}
},
methods: {
handleSizeChange(val) {
this.pageSize = val
this.fetchData()
},
handleCurrentChange(val) {
this.currentPage = val
this.fetchData()
},
fetchData() {
// 调用API获取数据
api.getList({
page: this.currentPage,
size: this.pageSize
}).then(res => {
this.list = res.data.list
this.total = res.data.total
})
}
}
}
</script>
自定义分页组件
如果需要完全自定义的分页组件,可以这样实现:
<template>
<div class="pagination">
<button
@click="prevPage"
:disabled="currentPage === 1">
上一页
</button>
<span
v-for="page in pages"
:key="page"
@click="goToPage(page)"
:class="{ active: page === currentPage }">
{{ page }}
</span>
<button
@click="nextPage"
: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: {
prevPage() {
if (this.currentPage > 1) {
this.$emit('page-changed', this.currentPage - 1)
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.$emit('page-changed', this.currentPage + 1)
}
},
goToPage(page) {
this.$emit('page-changed', page)
}
}
}
</script>
<style>
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
}
.pagination span {
margin: 0 5px;
cursor: pointer;
}
.pagination span.active {
font-weight: bold;
color: #409EFF;
}
</style>
无限滚动分页
对于移动端或需要流畅滚动体验的场景,可以使用无限滚动:
<template>
<div class="infinite-list" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy">
<div v-for="item in items" :key="item.id">
<!-- 内容展示 -->
</div>
<div v-if="loading" class="loading">加载中...</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [],
page: 1,
busy: false,
loading: false,
hasMore: true
}
},
methods: {
loadMore() {
if (this.busy || !this.hasMore) return
this.busy = true
this.loading = true
api.getList({
page: this.page
}).then(res => {
this.items = [...this.items, ...res.data.list]
this.page++
this.hasMore = res.data.hasMore
this.busy = false
this.loading = false
})
}
}
}
</script>
分页逻辑优化
对于大型数据集,可以考虑以下优化:
// 在计算属性中添加页码范围限制
visiblePages() {
const range = []
const start = Math.max(1, this.currentPage - 2)
const end = Math.min(this.totalPages, this.currentPage + 2)
for (let i = start; i <= end; i++) {
range.push(i)
}
if (start > 1) {
range.unshift(1)
if (start > 2) {
range.splice(1, 0, '...')
}
}
if (end < this.totalPages) {
range.push(this.totalPages)
if (end < this.totalPages - 1) {
range.splice(range.length - 1, 0, '...')
}
}
return range
}
以上实现方式可以根据项目需求选择或组合使用,Element UI的分页适合快速开发,自定义分页组件提供更大的灵活性,无限滚动则适合移动端体验。







