当前位置:首页 > VUE

vue中分页的实现

2026-01-20 06:37:54VUE

分页组件实现

在Vue中实现分页功能通常需要结合后端API的分页数据返回和前端的分页组件展示。以下是常见的实现方法:

基础分页组件

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

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

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

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

      if (end - start + 1 < this.maxVisible) {
        start = Math.max(1, end - this.maxVisible + 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>
.pagination {
  display: flex;
  gap: 5px;
}
.active {
  background-color: #42b983;
  color: white;
}
</style>

结合API请求

在父组件中使用分页组件并与API交互:

vue中分页的实现

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

    <!-- 分页组件 -->
    <Pagination
      :current-page="currentPage"
      :total-pages="totalPages"
      @page-changed="fetchData"
    />
  </div>
</template>

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

export default {
  components: { Pagination },
  data() {
    return {
      items: [],
      currentPage: 1,
      totalPages: 1,
      pageSize: 10
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData(page = 1) {
      this.currentPage = page
      const response = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.items = response.data.items
      this.totalPages = Math.ceil(response.data.total / this.pageSize)
    }
  }
}
</script>

使用第三方库

对于快速实现,可以考虑使用现成的分页组件库:

  1. 安装Element UI分页组件:

    vue中分页的实现

    npm install element-ui
  2. 使用示例:

    
    <template>
    <el-pagination
     @current-change="handleCurrentChange"
     :current-page="currentPage"
     :page-size="pageSize"
     :total="total"
     layout="prev, pager, next"
    >
    </el-pagination>
    </template>
import { ElPagination } from 'element-ui'

export default { components: { ElPagination }, data() { return { currentPage: 1, pageSize: 10, total: 100 } }, methods: { handleCurrentChange(val) { this.currentPage = val this.fetchData() } } }

```

无限滚动分页

对于移动端或需要流畅滚动体验的场景,可以实现无限滚动分页:

<template>
  <div class="infinite-list" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy">
    <div v-for="item in items" :key="item.id">{{ item.name }}</div>
    <div v-if="loading" class="loading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      loading: false,
      hasMore: true
    }
  },
  methods: {
    async loadMore() {
      if (this.loading || !this.hasMore) return

      this.loading = true
      const response = await axios.get('/api/items', {
        params: { page: this.page }
      })

      this.items = [...this.items, ...response.data.items]
      this.page++
      this.hasMore = response.data.hasMore
      this.loading = false
    }
  }
}
</script>

这些方法覆盖了Vue中实现分页的主要场景,可以根据具体需求选择合适的实现方式。

标签: 分页vue
分享给朋友:

相关文章

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…

vue实现单击

vue实现单击

Vue 实现单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或简写的 @ 符号绑定事件处理器。以下是几种常见的实现方式: 基础用法 通过 v-on:click 或 @click 绑…

vue elementui实现分页

vue elementui实现分页

使用 Element UI 实现 Vue 分页功能 Element UI 提供了 el-pagination 组件,可以方便地实现分页功能。以下是具体实现步骤: 安装 Element UI 确保项目…

vue响应式实现

vue响应式实现

Vue 响应式实现原理 Vue 的响应式系统是其核心特性之一,通过数据劫持和依赖收集实现。以下是 Vue 2.x 和 Vue 3.x 响应式实现的主要机制: Vue 2.x 的响应式实现 V…