1、\src\components\Pagination\index.vue
<!--
* @Description: 分页
* @Author: Dragon
* @Date: 2021-03-09 13:32:37
* @LastEditTime: 2021-03-16 15:25:42
* @LastEditors: Dragon
-->
<template>
<div :class="{'hidden':hidden}" class="pagination-container new-container">
<el-pagination
background
:current-page.sync="currentPage"
:page-size.sync="pageSize"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
:hide-on-single-page="total%pageSize>0"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script>
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number,
},
page: {
type: Number,
default: 1,
},
limit: {
type: Number,
default: 10,
},
pageSizes: {
type: Array,
default() {
return [10, 20, 30, 50]
},
},
layout: {
type: String,
// default: 'total, sizes, prev, pager, next, jumper'
default: 'total, prev, pager, next, jumper',
},
background: {
type: Boolean,
default: true,
},
autoScroll: {
type: Boolean,
default: true,
},
hidden: {
type: Boolean,
default: false,
},
},
computed: {
currentPage: {
get() {
return this.page
},
set(val) {
this.$emit('update:page', val)
},
},
pageSize: {
get() {
return this.limit
},
set(val) {
this.$emit('update:limit', val)
},
},
},
methods: {
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, limit: this.pageSize })
},
},
}
</script>
<style scoped lang='scss'>
.new-container {
margin: 20px 0 0;
text-align: right;
.el-pagination {
display: inline-block;
}
.pagination-container.hidden {
display: none;
}
}
</style>
2、页面调用
import Pagination from '@/components/Pagination'
components: { Pagination },
<pagination
:total="total" // 总条数
:layout="'total, prev, pager, next, jumper'" // 需要展示的选项
:page.sync="listQuery.data.page" // 当前页码
:limit.sync="listQuery.data.pagesize" // 每页展示几条
@pagination="refresh" // 每次触发事件,请求接口
/>