vue3全局封装table分页

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();
};
相关推荐
Calm5507 分钟前
ele表单未输入值提示为英文
前端
老华带你飞14 分钟前
校务管理|基于springboot 校务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring
JosieBook17 分钟前
【部署】Spring Boot + Vue框架项目生产环境部署完整方案
vue.js·spring boot·后端
爪洼守门员22 分钟前
前端性能优化
开发语言·前端·javascript·笔记·性能优化
TOYOAUTOMATON28 分钟前
GTH系列模组介绍
前端·目标检测·自动化
2022.11.7始学前端33 分钟前
n8n第十节 把Markdown格式的会议纪要发到企微
前端·chrome·n8n
fruge1 小时前
Lodash 源码精读:防抖节流的实现细节与边界场景
前端
yuzhiboyouye1 小时前
怎么熟悉一个web前端项目的业务呢?
前端
GISer_Jing1 小时前
AI在前端营销和用户增长领域应用(待补充)
前端·人工智能
橘子海全栈攻城狮1 小时前
【最新源码】基于springboot的会议室预订系统设计与实现 014
java·开发语言·前端·spring boot·后端·spring·自动化