温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现vue3封装自己的分页组件

发布时间:2021-09-28 10:49:03 来源:亿速云 阅读:181 作者:iii 栏目:开发技术

本篇内容主要讲解“如何实现vue3封装自己的分页组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现vue3封装自己的分页组件”吧!

背景

在浏览列表类型的数据的时候,如果数据比较多一次性全部请求会出现性能损耗以及加载延迟等问题,那么此时分页组件就起到了关键作用,它可以只请求一部分数据,也不会占用太多的页面空间,想看别的数据可以通过页码的改变来发起请求,刷新页面数据

现在我们自己来封装分页组件

组件所需参数

  • total 属性 :用来传递数据总条数

  • pagesize 属性 :每页展示几条数据

  • currentPage 属性 :当前默认页码

  • change-page 事件 :页码改变时触发的事件,参数为当前页码

组件落地代码my-pagination.vue

<template>
  <div class="my-pagination">
    <a href="javascript:;" :class="{ disabled: currentPage === 1 }" @click="changePage(false)">上一页</a>
    <span v-if="currentPage > 3">...</span>
    <a
      href="javascript:;" 
      v-for="item in list"
      :key="item"
      :class="{ active: currentPage === item }"
      @click="changePage(item)"
      >{{ item }}</a
    >
    <span v-if="currentPage < pages - 2">...</span>
    <a href="javascript:;" :class="{ disabled: currentPage === pages }" @click="changePage(true)">下一页</a>
  </div>
</template>
<script>
import { computed, ref } from 'vue-demi'
export default {
  name: 'MyPagination',
  props: {
    total: {
      type: Number,
      default: 80
    },
    pagesize: {
      type: Number,
      default: 10
    }
  },
  setup(props, { emit, attrs }) {
    // 当前页
    const currentPage = ref(attrs.currentPage)
    // 计算总页数
    const pages = computed(() => Math.ceil(props.total / props.pagesize))
    // 页码显示组合
    const list = computed(() => {
      const result = []
      // 总页数小于等于5页的时候
      if (pages <= 5) {
        for (let i = 1; i <= pages; i++) {
          result.push(i)
        }
      } else {
        // 总页数大于5页的时候
        // 控制两个极端那边的省略号的有无,页码的显示个数与选中页码居中
        if (currentPage.value <= 2) {
          for (let i = 1; i <= 5; i++) {
            result.push(i)
          }
        } else if (currentPage.value >= 3 && currentPage.value <= pages.value - 2) {
          for (let i = currentPage.value - 2; i <= currentPage.value + 2; i++) {
            result.push(i)
          }
        } else if (currentPage.value > pages.value - 2) {
          for (let i = pages.value - 4; i <= pages.value; i++) {
            result.push(i)
          }
        }
      }
      return result
    })
    // 点击上一页下一页页码改变页码
    const changePage = type => {
      // 点击上一页按钮
      if (type === false) {
        if (currentPage.value <= 1) return
        currentPage.value -= 1
      } else if (type === true) {
        // 点击下一页按钮
        if (currentPage.value >= pages.value) return
        currentPage.value += 1
      } else {
        // 点击页码
        currentPage.value = type
      }
      // 传给父组件当前页码,可以在该事件中做相关操作
      emit('change-page', currentPage.value)
    }
    return { currentPage, pages, list, changePage }
  }
}
</script>
<style scoped lang="less">
.my-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

使用组件

<XtxPagination 
:total="total" 
:pagesize="reqParams.pagesize" 
:currentPage="1" 
@change-page="changePage" />

效果

如何实现vue3封装自己的分页组件

到此,相信大家对“如何实现vue3封装自己的分页组件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI