vue分页代码实现
Vue 分页实现方法
基础分页组件实现
创建分页组件 Pagination.vue,包含页码切换逻辑:

<template>
<div class="pagination">
<button
@click="changePage(currentPage - 1)"
:disabled="currentPage === 1"
>上一页</button>
<span
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: currentPage === page }"
>{{ page }}</span>
<button
@click="changePage(currentPage + 1)"
:disabled="currentPage === totalPages"
>下一页</button>
</div>
</template>
<script>
export default {
props: {
totalItems: Number,
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>
<style>
.pagination {
display: flex;
gap: 5px;
}
.active {
color: red;
font-weight: bold;
}
</style>
在父组件中使用分页
<template>
<div>
<ul>
<li v-for="item in paginatedItems" :key="item.id">
{{ item.name }}
</li>
</ul>
<Pagination
:total-items="items.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 {
items: [], // 从API获取的数据
itemsPerPage: 5,
currentPage: 1
}
},
computed: {
paginatedItems() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.items.slice(start, end)
}
},
methods: {
handlePageChange(page) {
this.currentPage = page
},
fetchItems() {
// API请求获取数据
// this.items = response.data
}
},
created() {
this.fetchItems()
}
}
</script>
高级分页优化
对于大数据量分页,可添加页码省略功能:

// 在Pagination组件的computed中替换pages计算
pages() {
const range = []
const maxVisible = 5 // 最多显示页码数
let start = 1
let end = this.totalPages
if (this.totalPages > maxVisible) {
const half = Math.floor(maxVisible / 2)
start = Math.max(this.currentPage - half, 1)
end = Math.min(start + maxVisible - 1, this.totalPages)
if (end - start + 1 < maxVisible) {
start = end - maxVisible + 1
}
}
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
服务器端分页实现
当数据量极大时,应采用服务器端分页:
methods: {
async fetchPaginatedItems() {
const response = await axios.get('/api/items', {
params: {
page: this.currentPage,
per_page: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total
},
handlePageChange(page) {
this.currentPage = page
this.fetchPaginatedItems()
}
}
分页样式优化
使用CSS框架如Bootstrap可快速美化分页:
<template>
<nav aria-label="Page navigation">
<ul class="pagination">
<li class="page-item" :class="{ disabled: currentPage === 1 }">
<a class="page-link" href="#" @click.prevent="changePage(currentPage - 1)">Previous</a>
</li>
<li
v-for="page in pages"
:key="page"
class="page-item"
:class="{ active: currentPage === page }"
>
<a class="page-link" href="#" @click.prevent="changePage(page)">{{ page }}</a>
</li>
<li class="page-item" :class="{ disabled: currentPage === totalPages }">
<a class="page-link" href="#" @click.prevent="changePage(currentPage + 1)">Next</a>
</li>
</ul>
</nav>
</template>
以上实现涵盖了Vue分页的主要场景,包括客户端分页、服务器端分页以及样式优化方案。可根据实际项目需求选择适合的实现方式。






