vue翻页实现
Vue 翻页实现方法
基础分页组件实现
创建分页组件模板,包含页码按钮、上一页/下一页按钮:
<template>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<button
v-for="page in pages"
:key="page"
@click="goToPage(page)"
:class="{ active: page === currentPage }"
>
{{ page }}
</button>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</template>
计算总页数和显示页码数组:

<script>
export default {
props: {
totalItems: Number,
itemsPerPage: Number,
currentPage: Number
},
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: {
goToPage(page) {
this.$emit('page-changed', page)
},
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)
}
}
}
}
</script>
与数据列表配合使用
父组件中使用分页组件并处理数据分页:
<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="onPageChange"
/>
</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: {
onPageChange(page) {
this.currentPage = page
}
}
}
</script>
高级分页功能实现
添加页码省略和范围限制:

computed: {
pages() {
const range = []
const maxVisible = 5 // 最大显示页码数
let start = 1
let end = this.totalPages
if (this.totalPages > maxVisible) {
start = Math.max(1, this.currentPage - 2)
end = Math.min(this.totalPages, this.currentPage + 2)
if (start > 1) {
range.push(1)
if (start > 2) range.push('...')
}
for (let i = start; i <= end; i++) {
range.push(i)
}
if (end < this.totalPages) {
if (end < this.totalPages - 1) range.push('...')
range.push(this.totalPages)
}
} else {
for (let i = 1; i <= this.totalPages; i++) {
range.push(i)
}
}
return range
}
}
与API请求集成
在父组件中添加API请求逻辑:
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)
}
},
onPageChange(page) {
this.currentPage = page
this.fetchData(page)
}
},
created() {
this.fetchData()
}
样式优化
添加基本CSS样式:
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
border: 1px solid #ddd;
background: #fff;
cursor: pointer;
}
.pagination button.active {
background: #42b983;
color: white;
border-color: #42b983;
}
.pagination button:disabled {
opacity: 0.5;
cursor: not-allowed;
}






