当前位置:首页 > VUE

vue 分页 实现

2026-02-10 12:00:03VUE

分页组件实现

在Vue中实现分页功能通常需要结合后端API和前端组件。以下是两种常见的实现方式:

基于Element UI的分页

Element UI提供了现成的分页组件,可以快速集成:

<template>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10, 20, 50, 100]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total">
  </el-pagination>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      list: []
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      // 调用API获取数据
      api.getList({
        page: this.currentPage,
        size: this.pageSize
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.total
      })
    }
  }
}
</script>

自定义分页组件

如果需要完全自定义的分页组件,可以这样实现:

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1">
      上一页
    </button>

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

    <button 
      @click="nextPage" 
      :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: {
    prevPage() {
      if (this.currentPage > 1) {
        this.$emit('page-changed', this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.$emit('page-changed', this.currentPage + 1)
      }
    },
    goToPage(page) {
      this.$emit('page-changed', page)
    }
  }
}
</script>

<style>
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
}
.pagination span {
  margin: 0 5px;
  cursor: pointer;
}
.pagination span.active {
  font-weight: bold;
  color: #409EFF;
}
</style>

无限滚动分页

对于移动端或需要流畅滚动体验的场景,可以使用无限滚动:

<template>
  <div class="infinite-list" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy">
    <div v-for="item in items" :key="item.id">
      <!-- 内容展示 -->
    </div>
    <div v-if="loading" class="loading">加载中...</div>
  </div>
</template>

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

      this.busy = true
      this.loading = true

      api.getList({
        page: this.page
      }).then(res => {
        this.items = [...this.items, ...res.data.list]
        this.page++
        this.hasMore = res.data.hasMore
        this.busy = false
        this.loading = false
      })
    }
  }
}
</script>

分页逻辑优化

对于大型数据集,可以考虑以下优化:

// 在计算属性中添加页码范围限制
visiblePages() {
  const range = []
  const start = Math.max(1, this.currentPage - 2)
  const end = Math.min(this.totalPages, this.currentPage + 2)

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

  if (start > 1) {
    range.unshift(1)
    if (start > 2) {
      range.splice(1, 0, '...')
    }
  }

  if (end < this.totalPages) {
    range.push(this.totalPages)
    if (end < this.totalPages - 1) {
      range.splice(range.length - 1, 0, '...')
    }
  }

  return range
}

以上实现方式可以根据项目需求选择或组合使用,Element UI的分页适合快速开发,自定义分页组件提供更大的灵活性,无限滚动则适合移动端体验。

vue 分页 实现

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

相关文章

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: da…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue模版实现

vue模版实现

Vue 模板实现方法 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将 DOM 绑定至底层 Vue 实例的数据。以下是几种常见的 Vue 模板实现方式: 单文件组件(SFC) 使用 .v…

vue实现settimeout

vue实现settimeout

在 Vue 中实现 setTimeout Vue 中可以通过 JavaScript 原生的 setTimeout 方法实现延时操作,但需要注意结合 Vue 的响应式特性和生命周期管理。以下是几种常见实…

vue博客实现

vue博客实现

Vue 博客实现步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,安装基础依赖如 vue-router 和状态管理库(如 pinia)。配置路由文件实现页面跳转逻辑,例如博客首…