当前位置:首页 > VUE

vue组件实现分页复用

2026-02-24 20:17:30VUE

分页组件封装思路

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

基础分页组件实现

创建Pagination.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) } }


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变量增强自定义能力:

vue组件实现分页复用

<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>

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

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

相关文章

php实现分页

php实现分页

PHP实现分页的基本方法 使用原生PHP实现分页功能需要结合数据库查询和HTML展示。以下是核心实现步骤: 数据库查询与分页逻辑 // 连接数据库 $conn = new mysqli('loca…

Vue组件实现方法

Vue组件实现方法

Vue组件的基本实现方法 Vue组件是Vue.js的核心概念之一,用于构建可复用的UI模块。以下是Vue组件的几种常见实现方式: 单文件组件(SFC) 使用.vue文件格式,将模板、脚本和样式封装在…

vue实现下拉框组件

vue实现下拉框组件

Vue 下拉框组件实现 基础实现 创建基础下拉框组件,使用v-model实现双向数据绑定: <template> <div class="dropdown"> &…

vue组件实现

vue组件实现

Vue 组件实现 Vue 组件是 Vue.js 的核心特性之一,允许开发者将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种常见方式: 单文件组件(SFC) 单文件组件是 Vue 中…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref="…