当前位置:首页 > 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实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vue…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…