vue实现换页功能
Vue 实现分页功能
使用第三方库(如 Element UI)
安装 Element UI:
npm install element-ui
在 main.js 中引入:
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
在组件中使用 el-pagination:
<template>
<div>
<el-pagination
@current-change="handleCurrentChange"
: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: 100
};
},
methods: {
handleCurrentChange(val) {
this.currentPage = val;
// 加载对应页面的数据
this.fetchData();
},
fetchData() {
// 根据 currentPage 和 pageSize 获取数据
console.log(`加载第 ${this.currentPage} 页数据`);
}
}
};
</script>
自定义分页组件
创建一个自定义分页组件 Pagination.vue:
<template>
<div class="pagination">
<button
@click="changePage(currentPage - 1)"
:disabled="currentPage === 1">
上一页
</button>
<span v-for="page in pages" :key="page">
<button
@click="changePage(page)"
:class="{ active: currentPage === page }">
{{ page }}
</button>
</span>
<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>
<!-- 显示数据 -->
<Pagination
:total-items="totalItems"
: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: 10,
totalItems: 100
};
},
methods: {
handlePageChange(page) {
this.currentPage = page;
this.fetchData();
},
fetchData() {
console.log(`加载第 ${this.currentPage} 页数据`);
}
}
};
</script>
结合后端 API 实现分页
在 methods 中调用 API:
methods: {
async fetchData() {
try {
const response = await axios.get('/api/data', {
params: {
page: this.currentPage,
limit: this.pageSize
}
});
this.dataList = response.data.items;
this.total = response.data.total;
} catch (error) {
console.error('获取数据失败:', error);
}
}
}
分页样式优化
添加 CSS 样式:
.pagination {
margin-top: 20px;
display: flex;
justify-content: center;
align-items: center;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
border: 1px solid #ddd;
background: #fff;
cursor: pointer;
}
.pagination button:hover:not(:disabled) {
background: #f5f5f5;
}
.pagination button:disabled {
cursor: not-allowed;
opacity: 0.5;
}
.pagination button.active {
background: #42b983;
color: white;
border-color: #42b983;
}






