【組件封裝】二次封裝el-pagination,拒絕繁瑣配置

show1.jpg

el-pagination 分頁組件

在中后臺系統(tǒng)中,經(jīng)常會使用分頁器組件。el-pagination 需要配置
page-size、layout、@current-change等諸多屬性。每個頁面都這樣子使用的話既容易出現(xiàn)錯誤,也會造成代碼冗余

show2.jpg

組件設(shè)計(完整版)

p-el-pagination 默認的屬性,如background、small、page-sizes等,請根據(jù)實際業(yè)務(wù)修改默認值

調(diào)用效果及代碼

show3.png
<!--
 * @Date: 2022-05-11 14:38:32
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-11 14:50:41
 * @LastEditors: surewinT 840325271@qq.com
 * @Description:
-->

<template>
    <div class="">
        <p-el-pagination
            :total="total"
            :size="100"
            @handlePageSizeChange="handlePageSizeChange"
        />
    </div>
</template>

<script>
import Pagination from './p-el-pagination.vue';
export default {
    components: {
        'p-el-pagination': Pagination,
    },
    props: [],
    data() {
        return {
            total: 500,
        };
    },
    mounted() {},
    watch: {},
    methods: {
        handlePageSizeChange(page, size) {
            console.log('分頁器變化:', page, size);
        },
    },
};
</script>

<style lang='scss' scoped>
</style>

組件源碼(p-el-pagination .vue)

<!--
 * @Date: 2021-09-27 17:17:18
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-11 14:50:49
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 分頁器組件
-->

<template>
    <div class="" style="text-align: center">
        <el-pagination
            @size-change="sizeChange"
            @current-change="pageChange"
            :current-page="currentPage"
            :page-sizes="[30, 50, 100, 200, 500]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            background
            small
        >
        </el-pagination>
    </div>
</template>

<script>
export default {
    components: {},
    props: {
        total: {
            type: Number,
            default: 0,
        },
        size: {
            type: Number,
            default: 30,
        },
        page: {
            type: Number,
            default: 1,
        },
    },
    data() {
        return {
            currentPage: 1,
            currentsize: 30,
        };
    },
    mounted() {
        this.currentsize = this.size;
        this.currentPage = this.page;
    },
    watch: {
        size() {
            this.currentsize = this.size;
        },
        page() {
            this.currentPage = this.page;
        },
    },
    methods: {
        sizeChange(val) {
            this.currentsize = val;
            // size變化時,默認定位到第一頁
            this.currentPage = 1;
            this.$emit(
                'handlePageSizeChange',
                this.currentPage,
                this.currentsize
            );
        },
        pageChange(val) {
            this.currentPage = val;
            this.$emit(
                'handlePageSizeChange',
                this.currentPage,
                this.currentsize
            );
        },
    },
};
</script>

<style lang='scss' scoped>
::v-deep {
    .el-input__inner {
        height: 25px;
    }
}
</style>

倉庫源碼

封裝 el-pagination

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容