当前位置:首页 > 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实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script s…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echa…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…