vue3.0手动封装分页组件的方法

 更新时间:2021年09月26日 11:25:55   作者:勇敢牛牛,冲冲冲  
这篇文章主要为大家详细介绍了vue3.0手动封装分页组件的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue3.0手动封装分页组件的具体代码,供大家参考,具体内容如下

1.父组件引入

src/views/goods/components/goods-comment.vue

<!-- page表示初始化分页时,默认显示第几页 -->
    <XtxPagination @change-page='changePage' :pagesize='reqParams.pageSize' :total='total' :page='1' />
    //调接口
 import {findCommentListByGoods } from '@/api/product.js'
 export default{
  setup(){
  // 筛选条件准备
    const reqParams = reactive({
      // 当前页码
      page: 1,
      // 每页的条数
      pageSize: 10,
      // 是否有图片
      hasPicture: null,
      // 筛选条件
      tag: null,
      // 排序的字段
      sortField: null
    })
    // 侦听参数的变化
    watch(reqParams, () => {
   findCommentListByGoods(goods.value.id, reqParams).then(ret => {
        total.value = ret.result.counts
        list.value = ret.result.items
      })
    }, {
      immediate: true
    })
    // 控制页码的变化
    const changePage = (page) => {
      // 修改分页参数,重新调用接口即可
      reqParams.page = page
    }
    
  }
 }

2.子组件

src/components/library/xtx-pagination.vue

<template>
  <div class="xtx-pagination">
    <a @click='changePage(false)' href="javascript:;" :class="{disabled: currentPage===1}">上一页</a>
    <span v-if='currentPage > 3'>...</span>
    <a @click='changePage(item)' href="javascript:;" :class='{active: currentPage===item}' v-for='item in list' :key='item'>{{item}}</a>
    <span v-if='currentPage < pages - 2'>...</span>
    <a @click='changePage(true)' href="javascript:;" :class='{disabled: currentPage===pages}'>下一页</a>
  </div>
</template>
<script>
import { computed, ref } from 'vue'

export default {
  name: 'XtxPagination',
  props: {
    total: {
      type: Number,
      default: 80
    },
    pagesize: {
      type: Number,
      default: 10
    }
    // 默认初始页码
    // page: {
    //   type: Number,
    //   default: 1
    // }
  },
  setup (props, { emit, attrs }) {
    // attrs表示父组件传递的属性,但是props没有接收的属性,这种属性不是响应式的
    // 动态计算中期的页码信息
    // 每页的条数
    // const pagesize = 8
    // 总页数
    let pages = Math.ceil(props.total / props.pagesize)
    // 当前页码
    // console.log(attrs.page)
    const currentPage = ref(attrs.page || 1)
    // 动态计算页码列表
    const list = computed(() => {
      // 当父组件传递total的值发生变化时,计算属性会重新计算
      pages = Math.ceil(props.total / props.pagesize)
      const result = []
      // 总页码小于等于5;大于5
      if (pages <= 5) {
        // 总页码小于等于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 >= pages - 1) {
          // 右侧临界值
          for (let i = pages - 4; i <= pages; i++) {
            result.push(i)
          }
        } else {
          // 中间的状态
          for (let i = currentPage.value - 2; i <= currentPage.value + 2; i++) {
            result.push(i)
          }
        }
      }
      return result
    })
    // 控制上一页和下一页变化
    const changePage = (type) => {
      if (type === false) {
        // 上一页
        // 页面是第一页时,禁止点击操作
        if (currentPage.value === 1) return
        if (currentPage.value > 1) {
          currentPage.value -= 1
        }
      } else if (type === true) {
        // 下一页
        // 页面是最后页时,禁止点击操作
        if (currentPage.value === pages) return
        if (currentPage.value < pages) {
          currentPage.value += 1
        }
      } else {
        // 点击页码
        currentPage.value = type
      }
      emit('change-page', currentPage.value)
    }
    return { list, currentPage, pages, changePage }
  }
}
</script>
<style scoped lang="less">
.xtx-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>

知识点:attrs表示父组件传递的属性,但是props没有接收的属性,这种属性不是响应式的(vue3新增)

3.实现效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  •  面试问题Vue双向数据绑定原理

     面试问题Vue双向数据绑定原理

    这篇文章主要介绍了 面试问题Vue双向数据绑定原理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Vue利用computer解决单项数据流的问题详解

    Vue利用computer解决单项数据流的问题详解

    Vue 是一个非常流行和强大的前端框架,它让我们可以用简洁和优雅的方式来构建用户界面,今天我们来分享一个 Vue 中非常经典的问题,也是一个非常实用的技巧,希望对大家有所帮助
    2023-07-07
  • vue项目中如何实现网页的截图功能 (html2canvas)

    vue项目中如何实现网页的截图功能 (html2canvas)

    这篇文章主要介绍了vue项目中如何实现网页的截图功能 (html2canvas),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Vue中的 DOM与Diff详情

    Vue中的 DOM与Diff详情

    这篇文章主要介绍了Vue中的 DOM与Diff详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-08-08
  • vue实现图片裁剪后上传

    vue实现图片裁剪后上传

    这篇文章主要为大家详细介绍了vue实现图片裁剪后上传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • Vue 2 和 Vue 3 中 toRefs函数的不用用法

    Vue 2 和 Vue 3 中 toRefs函数的不用用法

    Vue 是一款流行的JavaScript 框架,用于构建用户界面,在Vue2和 Vue3中,都存在一个名为toRefs的函数,但其行为在这两个版本中有所不同,这篇文章主要介绍了Vue2和Vue3中toRefs的区别,需要的朋友可以参考下
    2023-08-08
  • Vue中设置登录验证拦截功能的思路详解

    Vue中设置登录验证拦截功能的思路详解

    今天在做vue和springboot交互的一个项目的时候,想要基于前端实现一些只有登录验证之后才能访问某些页面的操作,所以在这里总结一下实现该功能的一个解决方案
    2021-10-10
  • vue项目关闭eslint校验

    vue项目关闭eslint校验

    eslint是一个JavaScript的校验插件,通常用来校验语法或代码的书写风格。这篇文章主要介绍了vue项目关闭eslint校验,需要的朋友可以参考下
    2018-03-03
  • 详解vue axios用post提交的数据格式

    详解vue axios用post提交的数据格式

    这篇文章主要介绍了详解vue axios用post提交的数据格式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 5个实用的Vue技巧分享

    5个实用的Vue技巧分享

    在这篇文章中,我们将探讨五个实用的 Vue 技巧,这些技巧可以使你日常使用 Vue 编程更高效、更富有成效,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-08-08

最新评论