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();
};
相关推荐
晓得迷路了4 分钟前
栗子前端技术周刊第 84 期 - Vite v7.0 beta、Vitest 3.2、Astro 5.9...
前端·javascript·vite
独立开阀者_FwtCoder7 分钟前
最全301/302重定向指南:从SEO到实战,一篇就够了
前端·javascript·vue.js
Moment16 分钟前
给大家推荐一个超好用的 Marsview 低代码平台 🤩🤩🤩
前端·javascript·github
小满zs20 分钟前
Zustand 第三章(状态简化)
前端·react.js
普宁彭于晏21 分钟前
元素水平垂直居中的方法
前端·css·笔记·css3
恋猫de小郭33 分钟前
为什么跨平台框架可以适配鸿蒙,它们的技术原理是什么?
android·前端·flutter
云浪36 分钟前
元素变形记:CSS 缩放函数全指南
前端·css
明似水1 小时前
用 Melos 解决 Flutter Monorepo 的依赖冲突:一个真实案例
前端·javascript·flutter
独立开阀者_FwtCoder1 小时前
stagewise:让AI与代码编辑器无缝连接
前端·javascript·github
清沫1 小时前
Cursor Rules 开发实践指南
前端·ai编程·cursor