vue翻页实现
Vue 翻页实现方法
在 Vue 中实现翻页功能通常需要结合分页组件和数据处理逻辑。以下是几种常见的实现方式:
使用 Element UI 分页组件
Element UI 提供了现成的分页组件 <el-pagination>,可以快速实现翻页功能:

<template>
<div>
<el-pagination
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-size="pageSize"
:total="total">
</el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
total: 100
}
},
methods: {
handleCurrentChange(val) {
this.currentPage = val
this.fetchData()
},
fetchData() {
// 根据 currentPage 和 pageSize 获取数据
}
}
}
</script>
自定义分页组件
如果需要自定义分页样式,可以手动实现分页逻辑:
<template>
<div class="pagination">
<button
@click="prevPage"
:disabled="currentPage === 1">
上一页
</button>
<span>第 {{ currentPage }} 页</span>
<button
@click="nextPage"
:disabled="currentPage === totalPages">
下一页
</button>
</div>
</template>
<script>
export default {
props: {
totalItems: Number,
itemsPerPage: Number
},
data() {
return {
currentPage: 1
}
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage)
}
},
methods: {
prevPage() {
if (this.currentPage > 1) {
this.currentPage--
this.$emit('page-changed', this.currentPage)
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++
this.$emit('page-changed', this.currentPage)
}
}
}
}
</script>
结合后端 API 实现
实际项目中通常需要从后端获取分页数据:

methods: {
async fetchData() {
try {
const response = await axios.get('/api/data', {
params: {
page: this.currentPage,
size: this.pageSize
}
})
this.items = response.data.items
this.total = response.data.total
} catch (error) {
console.error(error)
}
}
}
使用 Vuex 管理分页状态
在大型应用中,可以使用 Vuex 集中管理分页状态:
// store.js
const store = new Vuex.Store({
state: {
pagination: {
currentPage: 1,
pageSize: 10,
total: 0
}
},
mutations: {
setCurrentPage(state, page) {
state.pagination.currentPage = page
},
setTotal(state, total) {
state.pagination.total = total
}
}
})
性能优化技巧
对于大数据量列表,可以结合虚拟滚动技术:
<template>
<div style="height: 500px">
<virtual-list
:size="50"
:remain="10"
:data="visibleItems">
<!-- 列表项模板 -->
</virtual-list>
</div>
</template>
这些方法可以根据项目需求灵活组合使用,实现高效、用户友好的翻页功能。






