首页 VueJS EL-Pagination分页组件封装

EL-Pagination分页组件封装

作者:胡同里的砖头 围观群众:19 更新于:2025-01-07

在SRC下面建目录components/Pagination.vue,代码如下:

<template>
<el-scrollbar>
<div :class="{ hidden: hidden }" class="pagination">
<el-pagination
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:background="background"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</el-scrollbar>
</template>

<script setup lang="ts">
const props = defineProps({
total: {
required: true,
type: Number as PropType<number>,
default: 0,
},
page: {
type: Number,
default: 1,
},
limit: {
type: Number,
default: 20,
},
pageSizes: {
type: Array as PropType<number[]>,
default() {
return [10, 20, 30, 50]
},
},
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper',
},
background: {
type: Boolean,
default: true,
},
autoScroll: {
type: Boolean,
default: true,
},
hidden: {
type: Boolean,
default: false,
},
})

const emit = defineEmits(['pagination', 'update:page', 'update:limit'])

const currentPage = useVModel(props, 'page', emit)

const pageSize = useVModel(props, 'limit', emit)

function handleSizeChange(val: number) {
emit('pagination', { page: currentPage, limit: val })
}

function handleCurrentChange(val: number) {
currentPage.value = val
emit('pagination', { page: val, limit: props.limit })
}
</script>

<style lang="scss" scoped>
.pagination {
padding: 12px;

&.hidden {
display: none;
}
}
</style>
2.全局引用
// main.ts
import { createApp } from 'vue';
import App from './App.vue';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import Pagination from '@/components/Pagination.vue'; // 引入 Pagination 组件

const app = createApp(App);

// 注册 Element Plus
app.use(ElementPlus);

// 全局注册 Pagination 组件
app.component('Pagination', Pagination);

app.mount('#app');
3.在列表页中绑定
<pagination
v-if="total > 0"
v-model:total="total"
v-model:page="queryForm.startIndex"
v-model:limit="queryForm.pageSize"
@pagination="fetchData"
/>

  • 本文标题: EL-Pagination分页组件封装
  • 文章分类:【VueJS】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8