当前位置:首页 > VUE

vue分页组件的实现

2026-02-22 04:13:33VUE

基础分页组件实现

创建一个基本的Vue分页组件需要定义当前页码、总页数等核心属性,并通过事件触发页码切换。

模板部分

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

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

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

脚本部分

<script>
export default {
  props: {
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pages() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page)
      }
    }
  }
}
</script>

高级分页功能

对于数据量大的情况,需要优化分页显示逻辑,避免显示过多页码按钮。

计算属性优化

computed: {
  pages() {
    const current = this.currentPage
    const total = this.totalPages
    const range = 2 // 显示当前页前后各2页

    if (total <= 5) {
      return Array.from({ length: total }, (_, i) => i + 1)
    }

    let pages = []
    pages.push(1)

    if (current - range > 2) {
      pages.push('...')
    }

    const start = Math.max(2, current - range)
    const end = Math.min(total - 1, current + range)

    for (let i = start; i <= end; i++) {
      pages.push(i)
    }

    if (current + range < total - 1) {
      pages.push('...')
    }

    pages.push(total)
    return pages
  }
}

与表格数据结合

分页组件通常需要与表格数据配合使用,下面展示如何在实际场景中集成。

vue分页组件的实现

父组件使用示例

<template>
  <div>
    <table>
      <!-- 表格内容 -->
    </table>
    <pagination
      :total-items="totalItems"
      :items-per-page="perPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

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

export default {
  components: { Pagination },
  data() {
    return {
      currentPage: 1,
      perPage: 10,
      totalItems: 0,
      items: []
    }
  },
  methods: {
    async fetchData(page = 1) {
      const response = await api.getItems({
        page,
        limit: this.perPage
      })
      this.items = response.data
      this.totalItems = response.total
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData(page)
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

样式优化

为分页组件添加基础样式提升用户体验。

CSS样式

vue分页组件的实现

<style scoped>
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
}

.pagination button:hover:not(:disabled) {
  background: #eee;
}

.pagination button.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>

服务器端分页处理

当数据量极大时,需要实现服务器端分页以减少前端压力。

API请求示例

async fetchData(page = 1) {
  try {
    const response = await axios.get('/api/items', {
      params: {
        page,
        limit: this.perPage
      }
    })
    this.items = response.data.items
    this.totalItems = response.data.totalCount
  } catch (error) {
    console.error('获取数据失败:', error)
  }
}

性能优化技巧

对于大型数据集,可以采用虚拟滚动或懒加载等技术提升性能。

虚拟滚动实现思路

// 使用vue-virtual-scroller等库
<template>
  <RecycleScroller
    :items="visibleItems"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <!-- 渲染单条数据 -->
    </template>
  </RecycleScroller>
  <pagination 
    :total-items="totalItems"
    @page-changed="handlePageChange"
  />
</template>

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

相关文章

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用 Element UI 分页组件 Element UI 提供了现成的分页组件 el-pagination,适合快速集成。需要先安装 Element UI 库。 安装 Ele…

Vue组件实现方法

Vue组件实现方法

Vue组件的基本实现 Vue组件是Vue.js的核心概念之一,允许将UI拆分为独立可复用的代码片段。组件的实现方式有多种,以下是常见的几种方法。 单文件组件(SFC) 单文件组件以.vue为后缀,将…

vue 分页功能实现

vue 分页功能实现

分页功能实现方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页功能。 安装 Element UI: npm in…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…

vue实现组件刷新

vue实现组件刷新

强制重新渲染组件 使用 this.$forceUpdate() 方法可以强制 Vue 实例重新渲染,但不会更新子组件。适用于依赖非响应式数据的情况。 methods: { refreshComp…

vue实现搜索组件

vue实现搜索组件

Vue 实现搜索组件的方法 基础搜索组件实现 创建基础搜索组件需要包含输入框和搜索按钮。在Vue中可以使用v-model绑定输入值,通过@click或@keyup.enter触发搜索。 <te…