Vue + Element-UI管理系统中 Pagination 分页 二次封装
本文封装的分页组件是在Element-UI 的el-pagination基础之上封装的。
一.在components文件夹下,新建pagination文件
<template> <div class="page-content"> <el-pagination :background="background" :page-sizes="pageSizes" :layout="layout" :current-page.sync="currentPage" :page-size.sync="pageSize" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"> </el-pagination> </div> </template> <script> export default { name: 'Pagination', props: { background: { type: Boolean, default: true }, pageSizes: { type: Array, default: () => { return [10, 20, 30, 50] } }, layout: { type: String, default: 'total, sizes, prev, pager, next, jumper' }, total: { required: true, type: Number, default: 0 }, pageNum: { type: Number, default: 1 }, limit: { type: Number, default: 10 } }, computed: { currentPage: { get() { return this.pageNum }, set(val) { this.$emit('update:pageNum', val) } }, pageSize: { get() { return this.limit }, set(val) { this.$emit('update:limit', val) } } }, methods: { handleSizeChange(val) { this.currentPage = 1; this.$emit('pagination', { pageNum: this.currentPage, limit: val }) }, handleCurrentChange(val) { this.$emit('pagination', { pageNum: val, limit: this.pageSize }) } } } </script> <style scoped> .page-content{ text-align: right; margin: 10px 10px 0 0; } </style>
二、使用组件
1、引入import Pagination from '@/components/pagination';
2、注册组件components: { Pagination };
3、使用组件
<Pagination :total="total" :pageNum.sync="pages.pageNum" :limit.sync="pages.pageSize" @pagination="fetchData()" />
mounted() { this.fetchData() }, methods: { fetchData() { this.loading = true getList(this.pages).then(res => { if(res.data.code === 20000) { this.dataList = res.data.data.items this.total = res.data.data.total this.loading = false } }) setTimeout(() => { this.loading = false },3000) } }
三、参数说明
total:数据总条数,
pageNum: 页数
pageSize:每页展示条数
pagination:页码改变或每页条数改变,所触发的函数
源码
如果你感兴趣的话,请前往 GitHub 查看源码和完整文档。
https://github.com/wangibook/my-table-component