Vue + Element-UI管理系统中 Pagination 分页 二次封装
本文封装的分页组件是在Element-UI 的el-pagination基础之上封装的。
一.在components文件夹下,新建pagination文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <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、使用组件
1 2 3 4 5 6 | <Pagination :total= "total" :pageNum.sync= "pages.pageNum" :limit.sync= "pages.pageSize" @pagination= "fetchData()" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!