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();
};
相关推荐
大腕先生18 分钟前
通用分页超详细介绍(附带源代码解析&页面展示效果)
xml·java·linux·服务器·开发语言·前端·idea
睿智的海鸥23 分钟前
Markdown 语法大全详解
开发语言·前端·javascript·css·html
Highcharts.js33 分钟前
用Highcharts如何动态向一个序列添加点
前端·javascript·react.js·highcharts
HookJames39 分钟前
设计Section 09 · Cost & Lead Time Factors 的完整 Block Editor 操作步骤
前端
玖玖passion1 小时前
React 常用 Hooks 函数及使用方法完全指南(useState / useEffect / useRef / useContext / useCallback / useMemo / useReducer)
前端·javascript
Awu12271 小时前
⚡精通Claude第6课-Hooks钩子系统:从前端视角玩转AI自动化工作流
前端·aigc·claude
椰猫子1 小时前
Spring Framework(Bean)
java·前端·spring
道清茗1 小时前
【RH294知识点汇总】第 7 章 《 使用角色和 Ansible 内容集合简化 Playbook 》
java·前端·ansible
前端那点事1 小时前
彻底弄懂async/await!解决回调地狱,Vue异步开发必备(超全实战)
前端·vue.js
A_nanda2 小时前
VS2022安装QT6.5.3后,如何更新项目配置
前端·javascript·vue.js