Vue3+element-ui + TS封装全局分页组件

本文介绍了如何使用Vue3、element-ui和TypeScript封装一个全局分页组件。

环境依赖

在开始之前,你需要安装以下环境:

  • Vue3
  • element-ui
  • TypeScript

组件功能

这个分页组件提供以下功能:

  • 支持自定义每页显示条数
  • 支持自定义跳转到指定页码
  • 支持显示总页数和总条数
  • 支持自定义样式

组件实现

分页组件结构

分页组件由以下几部分组成:

<template>
  <div class="pagination">
    <el-pagination
      :total="total"
      :page-size="pageSize"
      :current-page.sync="currentPage"
      :layout="'total, sizes, prev, pager, next, jumper'"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

分页组件属性

分页组件提供以下属性:

属性名类型默认值描述
totalNumber0总条数
pageSizeNumber10每页显示条数
currentPageNumber1当前页码

分页组件方法

分页组件提供以下方法:

方法名描述
handleSizeChange当每页显示条数发生变化时触发
handleCurrentChange当页码发生变化时触发

分页组件样式

你可以通过修改以下样式来自定义分页组件的样式:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 20px;
}

.el-pagination__sizes {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.el-pagination__jump {
  margin-left: 20px;
}

.el-pagination__total {
  margin-right: 20px;
}

使用分页组件

使用分页组件非常简单。你只需要在你的Vue组件中引入我们封装好的Pagination组件,然后在template中使用即可。

<template>
  <div class="page">
    <pagination
      :total="total"
      :page-size="pageSize"
      :current-page.sync="currentPage"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import Pagination from '@/components/Pagination.vue'

export default defineComponent({
  name: 'Page',
  components: {
    Pagination,
  },
  setup() {
    const total = ref(1000)
    const pageSize = ref(10)
    const currentPage = ref(1)

    return {
      total,
      pageSize,
      currentPage,
    }
  },
})
</script>

总结

本文介绍了如何使用Vue3、element-ui和TypeScript封装一个全局分页组件。你可以根据自己的需求来修改我们提供的分页组件样式和属性。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值