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();
};
相关推荐
excel38 分钟前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel2 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼3 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping3 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙4 小时前
[译] Composition in CSS
前端·css
白水清风4 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix4 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278004 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端4 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧4 小时前
Promise 的使用
前端·javascript