vue基本实现分页
Vue 分页实现方法
基础分页组件实现
创建一个基础的 Pagination 组件,包含页码按钮和导航功能:
<template>
<div class="pagination">
<button
@click="changePage(currentPage - 1)"
:disabled="currentPage === 1"
>
上一页
</button>
<button
v-for="page in pages"
:key="page"
@click="changePage(page)"
:class="{ active: currentPage === page }"
>
{{ page }}
</button>
<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>
<style>
.pagination button {
margin: 0 5px;
padding: 5px 10px;
}
.pagination button.active {
background-color: #42b983;
color: white;
}
</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 {
currentPage: 1,
itemsPerPage: 5,
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
// ...更多数据
]
}
},
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>
优化分页显示
改进页码显示逻辑,只显示当前页附近的页码:
// 在Pagination组件的computed中替换pages计算属性
pages() {
const range = []
const visiblePages = 5 // 可见页码数
let start = Math.max(1, this.currentPage - Math.floor(visiblePages / 2))
let end = Math.min(this.totalPages, start + visiblePages - 1)
if (end - start + 1 < visiblePages) {
start = Math.max(1, end - visiblePages + 1)
}
for (let i = start; i <= end; i++) {
range.push(i)
}
return range
}
与后端API集成
当数据来自后端API时,可以这样实现:
// 在父组件中
methods: {
async fetchData(page = 1) {
this.loading = true
try {
const response = await axios.get('/api/items', {
params: {
page,
per_page: this.itemsPerPage
}
})
this.items = response.data.data
this.totalItems = response.data.total
} catch (error) {
console.error(error)
} finally {
this.loading = false
}
},
handlePageChange(page) {
this.currentPage = page
this.fetchData(page)
}
},
created() {
this.fetchData()
}
使用第三方库
如果需要快速实现,可以使用现有分页库:
npm install vuejs-paginate
使用示例:
<template>
<paginate
:page-count="totalPages"
:click-handler="handlePageChange"
:prev-text="'上一页'"
:next-text="'下一页'"
:container-class="'pagination'"
:page-class="'page-item'"
>
</paginate>
</template>
<script>
import Paginate from 'vuejs-paginate'
export default {
components: {
Paginate
},
// ...其他代码保持不变
}
</script>
这些方法涵盖了从基础实现到高级集成的各种分页场景,可以根据项目需求选择适合的方案。







