当前位置:首页 > VUE

vue组件实现分页复用

2026-02-24 20:17:30VUE

分页组件封装思路

封装一个可复用的Vue分页组件需要考虑数据独立性、配置灵活性和事件通信。以下是一个典型实现方案:

基础分页组件实现

创建Pagination.vue组件文件:

vue组件实现分页复用

<template>
  <div class="pagination">
    <button 
      :disabled="currentPage <= 1" 
      @click="changePage(currentPage - 1)"
    >上一页</button>

    <span v-for="page in pageRange" :key="page">
      <button 
        :class="{ active: currentPage === page }"
        @click="changePage(page)"
      >{{ page }}</button>
    </span>

    <button 
      :disabled="currentPage >= totalPages" 
      @click="changePage(currentPage + 1)"
    >下一页</button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    },
    maxVisibleButtons: {
      type: Number,
      default: 5
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pageRange() {
      const range = []
      const half = Math.floor(this.maxVisibleButtons / 2)
      let start = Math.max(this.currentPage - half, 1)
      let end = Math.min(start + this.maxVisibleButtons - 1, this.totalPages)

      if (end - start + 1 < this.maxVisibleButtons) {
        start = Math.max(end - this.maxVisibleButtons + 1, 1)
      }

      for (let i = start; i <= end; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page)
      }
    }
  }
}
</script>

<style scoped>
.pagination {
  display: flex;
  gap: 5px;
}
.active {
  background-color: #42b983;
  color: white;
}
</style>

父组件调用方式

在需要使用分页的父组件中:

<template>
  <div>
    <!-- 数据列表展示 -->
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>

    <!-- 分页组件 -->
    <Pagination
      :total-items="dataList.length"
      :items-per-page="perPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from './Pagination.vue'

export default {
  components: { Pagination },
  data() {
    return {
      dataList: [], // 从API获取的全部数据
      perPage: 10,
      currentPage: 1
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.perPage
      const end = start + this.perPage
      return this.dataList.slice(start, end)
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
      // 如果需要可以在这里触发API请求
    }
  }
}
</script>

高级功能扩展

  1. 每页条数选择器
    
    <select v-model="itemsPerPage" @change="handlePerPageChange">
    <option value="5">5条/页</option>
    <option value="10">10条/页</option>
    <option value="20">20条/页</option>
    </select>

methods: { handlePerPageChange() { this.currentPage = 1 this.$emit('per-page-changed', this.itemsPerPage) } }

vue组件实现分页复用


2. 跳转到指定页码
```vue
<input 
  type="number" 
  :min="1" 
  :max="totalPages" 
  v-model.number="jumpPage"
  @keyup.enter="changePage(jumpPage)"
>
  1. 总条数显示
    <div class="total-count">共 {{ totalItems }} 条记录</div>

服务器分页适配

对于后端分页的场景,修改分页逻辑:

methods: {
  async handlePageChange(page) {
    this.currentPage = page
    const response = await api.fetchData({
      page,
      size: this.itemsPerPage
    })
    this.paginatedData = response.data
    this.totalItems = response.total
  }
}

样式自定义方案

通过插槽和CSS变量增强自定义能力:

<template>
  <div class="pagination" :style="cssVars">
    <slot name="prev" :page="currentPage" :disabled="currentPage <= 1">
      <button>上一页</button>
    </slot>
    <!-- ... -->
  </div>
</template>

<script>
props: {
  themeColor: {
    type: String,
    default: '#42b983'
  }
},
computed: {
  cssVars() {
    return {
      '--theme-color': this.themeColor
    }
  }
}
</script>

<style scoped>
.pagination button.active {
  background-color: var(--theme-color);
}
</style>

这种实现方式提供了高度可复用的分页组件,支持客户端分页和服务器分页两种模式,同时允许深度自定义样式和行为。

标签: 分页组件
分享给朋友:

相关文章

vue实现组件封装

vue实现组件封装

Vue 组件封装方法 封装组件的基本步骤 创建独立的 .vue 文件,包含 template、script 和 style 三个部分。通过 props 接收父组件传递的数据,使用 $emit 触发…

vue怎么实现组件缓存

vue怎么实现组件缓存

vue实现组件缓存的方法 在Vue中实现组件缓存可以通过内置的<keep-alive>组件完成,该组件能够缓存不活动的组件实例,避免重复渲染。 使用<keep-alive>…

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息…

vue实现下拉刷新组件

vue实现下拉刷新组件

Vue 下拉刷新组件实现 核心思路 通过监听 touch 事件计算滑动距离,结合 CSS 过渡动画实现下拉效果,触发刷新回调。 基础实现步骤 监听 touch 事件 在组件 mounted 阶段…

php实现分页

php实现分页

PHP实现分页的基本方法 使用LIMIT和OFFSET进行数据库查询分页。通过计算当前页码和每页显示数量,动态生成SQL语句。 $page = isset($_GET['page']) ? (in…

uniapp如何将底部导航组件化

uniapp如何将底部导航组件化

在 uniapp 中将底部导航组件化 将底部导航组件化可以提高代码复用性和维护性。以下是具体实现方法: 创建自定义底部导航组件 新建一个组件文件,例如 tab-bar.vue,放置在 comp…