vue手动封装分页组件

单独封装一个分页组件,进行全局注册,在需要使用的组件中进行引入,达到复用提高效率

     1. total,总条数 (外部使用Pagination组件的区域传递进来的数据)
     2. pageSize,每页显示多少条(外部使用Pagination组件的区域传递进来的数据)
     3. totalPage, 根据1,2派生出总页数
     4. lxyms,连续页码数(外部使用Pagination组件的区域传递进来的数据)
     5. currentPage,当前页(内部数据)
     6. startEnd,连续页码的起始页 & 最终页
     7. indexFromWrap: 外部逻辑传入的当前页下标

下列是所封装的子组件

<template>
  <div class="pagination">
    <button @click="updateCPage(currentPage-1)">上一页</button>
    <button v-if="startEnd.start>1" @click="updateCPage(1)">1</button>
    <button v-if="startEnd.start>2">...</button>
    <button
        v-for="item in totalPage"
        :class="{active:currentPage===item}"
        v-if="item>=startEnd.start && item<=startEnd.end"
        @click="updateCPage(item)">{{item}}</button>
    <button v-if="startEnd.end<totalPage-1" >···</button>
    <button v-if="startEnd.end<totalPage" @click="updateCPage(totalPage)">{{totalPage}}</button>
    <button @click="updateCPage(currentPage+1)">下一页</button>
    <button style="margin-left: 30px">{{totalPage}}</button>

  </div>
</template>

<script>
export default {
  /*
     1. total,总条数 (外部使用Pagination组件的区域传递进来的数据)
     2. pageSize,每页显示多少条(外部使用Pagination组件的区域传递进来的数据)
     3. totalPage, 根据1,2派生出总页数
     4. lxyms,连续页码数(外部使用Pagination组件的区域传递进来的数据)
     5. currentPage,当前页(内部数据)
     6. startEnd,连续页码的起始页 & 最终页

     7. indexFromWrap: 外部逻辑传入的当前页下标
 */
  name: "Pagination",
  //由父组件传递总条数 每页显示数  连续页码数  当前页下标
  props:["total","pageSize","lxyms","indexFromWrap"],
  data(){
    return{
      currentPage:this.indexFromWrap
    }
  },
  mounted() {
    console.log(this.totalPage===10)
  },
  computed:{
    //总页数
    totalPage(){
      return Math.ceil(this.total/this.pageSize)
    },

    //最终页-起始页 = 连续页码数-1
    startEnd(){
      let {currentPage,lxyms,totalPage} = this
      // return console.log(this)
      let start;
      let end;
      //计算连续页码的起始页
      start = currentPage-Math.floor(lxyms/2)
      start<1?start=1:""
      //结束页
      end=start + lxyms -1  //此时当start<1时 在计算end时已经进行补位了
      // end>totalPage ?end=totalPage:""
      if(end>totalPage){
        end=totalPage;
        start = end-lxyms+1;  //此处当结束位置超出时,用start进行补位

        start < 1 ? start = 1 :""  // 要满足start处于一个正常的范围
      }
      return {start,end}
    }
  },

  methods:{
    updateCPage(currentPage){
      //可将button中的判断抽离到此处
      if(currentPage<1) return;
      if(currentPage>1) return;
      if(currentPage===this.currentPage) return;
      this.currentPage = currentPage;
      //将当前页的数据,与外部的父组件进行数据交互
      this.$emit("updateCPage",this.currentPage)
    }
  },
  watch:{
    //监听当前页下标
    indexFromWrap:{
        handler(val){
          console.log(val);
          this.currentPage=val
        },
        deep:true,
      immediate:true
    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>

下列是父组件中调用,进行传参,如总条数,单页显示数,连续页码数

<template>
  <div>
    <Pagination :total="total" :pageSize="pageSize" :lxyms="lxyms" :indexFromWrap="indexFromWrap" @updateCPage="updateCPage"></Pagination>
  </div>
</template>

<script>
export default {
  name: "Search",
  data(){
    return{
      total:1000,
      pageSize:1,
      lxyms:5,
      indexFromWrap:6,
    }
  },
  methods:{
    updateCPage(index){
        // this.indexFromWrap=index
      console.log(index,"sss")
    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  margin: 100px auto;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中封装分页组件的方法与Vue2有所不同。以下是一个基本的封装分页组件的方法: 1. 首先,在你的项目中创建一个名为`Pagination.vue`的组件文件。 2. 在`Pagination.vue`组件中,你可以使用`<template>`标签来定义组件的结构。可以使用`<div>`标签来包裹分页组件的内容,比如页码和按钮。 3. 在`<script>`标签中,你需要导入`vue`并声明组件。你可以使用`ref`来追踪当前页码,并且使用`computed`属性来计算总页数。 4. 在组件内部,你可以创建一个`methods`对象,并在其中定义一些方法来处理页码的变化。比如,你可以创建`goToPage`方法来跳转到指定的页码。 5. 最后,在`<style>`标签中,你可以定义组件的样式,如页码的颜色和按钮的样式。 在你的项目中使用这个封装分页组件的方法如下: 1. 在需要使用分页功能的组件中,使用`import`关键字导入刚刚封装的`Pagination`组件。 2. 在`components`属性中注册`Pagination`组件。 3. 在`<template>`标签中使用自定义的分页组件。可以使用`v-model`指令来双向绑定当前页码。 通过上述步骤,你就可以在Vue3中封装一个分页组件并在项目中使用了。这个组件可以提供分页功能,让用户可以方便地切换页码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于Vue如何封装分页组件](https://download.csdn.net/download/weixin_38550605/12789728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue3 element-ui实现Pagination分页组件--封装分页](https://blog.csdn.net/coinisi_li/article/details/128952886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值