用vue怎样实现分页
使用 Vue 实现分页的方法
基本分页组件实现
在 Vue 中实现分页功能,可以通过自定义分页组件完成。以下是一个基础实现示例:
<template>
<div class="pagination">
<button
@click="prevPage"
:disabled="currentPage === 1"
>
上一页
</button>
<span v-for="page in pages" :key="page">
<button
@click="goToPage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</button>
</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 button {
margin: 0 5px;
padding: 5px 10px;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
.pagination button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</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: 10,
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
}
}
}
</script>
使用第三方库
对于更复杂的分页需求,可以考虑使用现成的 Vue 分页组件库:
-
vue-paginate:

npm install vue-paginate使用示例:
import VuePaginate from 'vue-paginate' Vue.use(VuePaginate) // 模板中使用 <paginate :page-count="totalPages" :click-handler="handlePageChange" :prev-text="'上一页'" :next-text="'下一页'" :container-class="'pagination'" > </paginate> -
v-pagination:

npm install v-pagination使用示例:
import VPagination from 'v-pagination' // 注册组件 components: { VPagination } // 模板中使用 <v-pagination v-model="currentPage" :page-count="totalPages" :classes="bootstrapPaginationClasses" :labels="customLabels" />
服务器端分页
当数据量很大时,建议实现服务器端分页:
methods: {
async fetchData(page = 1) {
try {
const response = await axios.get('/api/items', {
params: {
page,
per_page: this.itemsPerPage
}
})
this.items = response.data.items
this.totalItems = response.data.total
} catch (error) {
console.error(error)
}
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
},
created() {
this.fetchData()
}
优化分页显示
对于大量页数的情况,可以优化只显示部分页码:
computed: {
pages() {
const range = []
const current = this.currentPage
const total = this.totalPages
const delta = 2
let start = Math.max(2, current - delta)
let end = Math.min(total - 1, current + delta)
if (current - delta <= 2) {
end = 2 + delta * 2
}
if (current + delta >= total - 1) {
start = total - 1 - delta * 2
}
range.push(1)
if (start > 2) {
range.push('...')
}
for (let i = start; i <= end; i++) {
range.push(i)
}
if (end < total - 1) {
range.push('...')
}
if (total > 1) {
range.push(total)
}
return range
}
}
这些方法提供了从基础到高级的 Vue 分页实现方案,可以根据项目需求选择合适的实现方式。






