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();
};
相关推荐
毕设源码-朱学姐5 小时前
【开题答辩全过程】以 工厂能耗分析平台的设计与实现为例,包含答辩的问题和答案
java·vue.js
老前端的功夫6 小时前
Vue 3 性能深度解析:从架构革新到运行时的全面优化
javascript·vue.js·架构
天天扭码6 小时前
如何实现流式输出?一篇文章手把手教你!
前端·aigc·ai编程
前端 贾公子7 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
GISer_Jing8 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能
明远湖之鱼8 小时前
一种基于 Service Worker 的渐进式渲染方案的基本原理
前端
前端小端长9 小时前
Vue 中 keep-alive 组件的原理与实践详解
前端·vue.js·spring
FeelTouch Labs9 小时前
Nginx核心架构设计
运维·前端·nginx
雪球工程师团队9 小时前
别再“苦力”写后台,Spec Coding “跑” 起来
前端·ai编程