vue3 实现 el-pagination页面分页组件的封装以及调用

文章介绍了如何在Vue项目中使用ElementPlus的分页组件,并展示了如何动态绑定数据和处理分页事件。内容包括组件模板、TS编写、样式设置以及页面调用部分的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例图

一、组件代码

<template>
  <el-config-provider :locale="zhCn">
    <el-pagination background class="lj-paging" layout="prev, pager, next, jumper" :pager-count="5" :total="total"
      :current-page="pageIndex" :page-size="pageSize"
      @current-change="changePage" />
  </el-config-provider>
</template>
<script lang='ts' setup>
import zhCn from 'element-plus/es/locale/lang/zh-cn'
type TProps = {
  pageIndex: number
  pageSize: number
  total: number
}
const props = withDefaults(defineProps<TProps>(), {})

const emit = defineEmits(['onChangePage'])

const changePage = (page: number) => {
  emit('onChangePage', page)
}


</script>
<style lang='scss' scoped>
.lj-paging {
  :deep(button) {
    border-radius: 50%;
    margin: 0 4px;
    width: 40px;
    height: 40px;
    i{
      font-size: 20px !important;
    }
  }
  :deep(.el-pager) {
    li {
      width: 40px;
      height: 40px;
      line-height: 40px;
      font-size: 20px;
      border-radius: 50%;
      margin: 0 4px;
      color: #757B92;

      &.is-active {
        border: 1px solid #5C7DEE;
        background: #5C7DEE;
        box-shadow: 0 0 10px 0#6268FF;
        color: #ffffff;
        font-weight: normal;
      }
    }
  }
  :deep(.el-pagination__jump){
    font-size: 16px;
  }
  :deep(.el-input__inner){
    font-size: 20px !important;
  }
}
</style>

二、页面调用


<template>
  <div class="manage-scroll">
    <div class="manage-list">
      <div class="manage-item" v-for="(ma,maIndex) in manageList" :key="maIndex">
        <div class="item-info">
          <div class="item-title">{{ ma.title }}</div>
          <div class="item-des">
            <div>日&nbsp;&nbsp;&nbsp;期:{{ ma.date }}</div>
            <div>上传者:{{ ma.upload }}</div>
          </div>
        </div>
        <div class="item-btn flex">
          <img :src="dowmIcon" alt="">
          <span>下载</span>
        </div>
      </div>
    </div>
  </div>
  <div class="search-pagination">
    <lj-paging :total="datas.total" :pageIndex="datas.PageIndex" :pageSize="datas.PageSize" @on-change-page="onChangePage" />
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import dowmIcon from '@/assets/dowm_icon.png'
import ljPaging from '@/components/common/paging/index.vue'

const datas = reactive({
  PageIndex: 1,
  PageSize: 20,
  total: 100,
})
const manageList = ref<any>([
  {title: '新教师培训—英语场新教师培训—英语场新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'},
  {title: '新教师培训—英语场', upload: '张龙', date: '2023-08-28 09:25:00'}
])

const onChangePage = (page:number) => {
  datas.PageIndex = page
}

</script>
<style lang="scss" scoped>
.search-pagination{
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>

       希望我的愚见能够帮助你哦~,若有不足之处,还望指出,你们有更好的解决方法,欢迎大家在评论区下方留言支持,大家一起相互学习参考呀~

### Vue 3 中使用 `el-pagination` 控件的详细教程 在 Vue 3 中,`el-pagination` 是一个非常强大的分页控件,能够帮助开发者实现分页功能。以下是关于如何在 Vue 3 中使用 `el-pagination` 的详细说明。 #### 基础用法 以下是一个基础的 `el-pagination` 示例,展示了如何通过点击分页按钮切换页面,并动态更新当前页面的内容[^1]。 ```vue <template> <div> <h1 v-if="currentPage === 1">1</h1> <h1 v-if="currentPage === 2">2</h1> <h1 v-if="currentPage === 3">3</h1> <el-pagination background layout="prev, pager, next" :total="30" @current-change="handlePageChange" style="justify-content: center;" /> </div> </template> <script> export default { data() { return { currentPage: 1, }; }, methods: { handlePageChange(page) { this.currentPage = page; }, }, }; </script> ``` #### 高级用法:与后端交互 当需要与后端进行交互时,可以使用 `el-pagination` 的更多属性来实现更复杂的分页功能。以下示例展示了如何监听分页大小变化和当前页码变化,并将这些信息传递给后端以获取对应的数据[^2]。 ```vue <template> <el-pagination @size-change="changeSizeHandle" @current-change="currentChangeHandle" :current-page="currentPage" layout="total, sizes, prev, pager, next, jumper" :total="total" background /> </template> <script> export default { data() { return { currentPage: 1, total: 100, }; }, methods: { changeSizeHandle(size) { console.log("每页条数:", size); // 调用后端接口获取数据 }, currentChangeHandle(page) { this.currentPage = page; console.log("当前页码:", page); // 调用后端接口获取数据 }, }, }; </script> ``` #### 封装分页组件 为了提高代码复用性,可以将 `el-pagination` 封装为一个独立的子组件。以下是一个封装后的分页组件示例[^3]。 ```vue <template> <div> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.currentPage" :page-size="pageParams.pageSize" :page-sizes="pageSizes" layout="total, sizes, prev, pager, next, jumper" :total="total" /> </div> </template> <script setup> import { ref, reactive, defineEmits, defineProps } from "vue"; const emits = defineEmits(["pageChange"]); const props = defineProps({ total: { type: Number, default: 0, }, }); const pageParams = reactive({ currentPage: 1, pageSize: 10, }); const pageSizes = ref([10, 20, 30, 50, 100]); const handleSizeChange = (val) => { pageParams.pageSize = val; emits("pageChange", pageParams); }; const handleCurrentChange = (val) => { pageParams.currentPage = val; emits("pageChange", pageParams); }; </script> ``` #### 注意事项 - 如果在布局中包含 `sizes`,并且传入了 `page-size`,必须监听 `page-size` 变更的事件(如 `onUpdate:pageSize`),否则切换分页大小可能不会生效[^4]。 -Vue 3 中,推荐使用组合式 API (`<script setup>`) 来编写组件,以提高代码的可读性和复用性。 --- ### 示例代码总结 以上代码展示了从基础到高级的 `el-pagination` 使用方法,并且包括了一个封装后的分页组件示例。通过这些示例,可以轻松实现前端分页功能并与后端进行交互。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值