1.封装element-plus table分页
<template>
<el-pagination
:page-sizes="pageSizes"
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:total="totals"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:small="false"
:disabled="false"
:background="false"
layout="total, sizes, prev, pager, next, jumper"/>
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps({
totals: {
required: true,
type: Number,
default: 0,
},
page: {
type: Number,
default: 1,
},
size: {
type: Number,
default: 20,
},
pageSizes: {
type: Array,
default() {
return [ 10, 20, 30,40];
},
},
});
const emit = defineEmits(['update:page', 'update:size','pagination'])
const currentPage = computed({
get: () => {
return props.page
},
set: (val) => {
emit('update:page', val)
}
})
const pageSize = computed({
get: () => {
return props.size
},
set: (val) => {
emit('update:page', 1)
emit('update:size', val)
}
})
function handleSizeChange(val) {
if (currentPage.value * val > props.total) {
currentPage.value = 1
}
emit('pagination', { page: currentPage.value, size: val })
}
function handleCurrentChange(val) {
emit('pagination', { page: val, size: pageSize.value })
}
</script>
<style lang="scss" scoped>
</style>
2.全局注册
main.js
import Pagination from "@/components/element/Pagination.vue";
import App from './App.vue'
const app=createApp(App)
app.component('Pagination', Pagination)
3.页面使用
<div class="page-panel">
<Pagination
:totals="totalElements"
:page="query.pageNum"
:size="query.pageSize"
@pagination="handleQuery"
/>
</div>
const handleQuery = (data) => {
query.value.pageNum = data.page;
query.value.pageSize = data.size;
query();
};