vue 实现分页效果
使用 Element UI 的分页组件
Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页效果。需要先安装 Element UI 并引入分页组件。
<template>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 30, 40]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
total: 100
}
},
methods: {
handleSizeChange(val) {
this.pageSize = val
this.fetchData()
},
handleCurrentChange(val) {
this.currentPage = val
this.fetchData()
},
fetchData() {
// 根据 currentPage 和 pageSize 获取数据
}
}
}
</script>
自定义分页组件
如果需要自定义分页逻辑,可以手动实现分页效果。通过计算总页数和当前页数据来实现分页功能。
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<div class="pagination">
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
<button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
data: [], // 所有数据
currentPage: 1,
itemsPerPage: 10
}
},
computed: {
totalPages() {
return Math.ceil(this.data.length / this.itemsPerPage)
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.data.slice(start, end)
}
},
methods: {
prevPage() {
if (this.currentPage > 1) {
this.currentPage--
}
},
nextPage() {
if (this.currentPage < this.totalPages) {
this.currentPage++
}
}
}
}
</script>
使用第三方库 vue-paginate
vue-paginate 是一个轻量级的分页库,适合快速集成。需要先安装该库。
npm install vue-paginate
在 Vue 项目中注册插件并使用:
<template>
<div>
<ul>
<li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
</ul>
<paginate
v-model="currentPage"
:page-count="totalPages"
:click-handler="changePage"
:prev-text="'Prev'"
:next-text="'Next'"
:container-class="'pagination'">
</paginate>
</div>
</template>
<script>
import Paginate from 'vue-paginate'
export default {
components: {
Paginate
},
data() {
return {
data: [],
currentPage: 1,
itemsPerPage: 10
}
},
computed: {
totalPages() {
return Math.ceil(this.data.length / this.itemsPerPage)
},
paginatedData() {
const start = (this.currentPage - 1) * this.itemsPerPage
const end = start + this.itemsPerPage
return this.data.slice(start, end)
}
},
methods: {
changePage(pageNum) {
this.currentPage = pageNum
}
}
}
</script>
结合后端 API 实现分页
如果数据来自后端 API,可以通过传递分页参数(如 page 和 pageSize)来实现分页。
<template>
<div>
<el-pagination
@current-change="handlePageChange"
:current-page="currentPage"
:page-size="pageSize"
:total="total"
layout="prev, pager, next">
</el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
pageSize: 10,
total: 0,
data: []
}
},
created() {
this.fetchData()
},
methods: {
fetchData() {
const params = {
page: this.currentPage,
pageSize: this.pageSize
}
axios.get('/api/data', { params }).then(response => {
this.data = response.data.list
this.total = response.data.total
})
},
handlePageChange(page) {
this.currentPage = page
this.fetchData()
}
}
}
</script>






