当前位置:首页 > VUE

vue中如何实现分页

2026-01-22 03:07:05VUE

实现基础分页功能

在Vue中实现分页通常需要结合后端API返回的分页数据。前端主要处理页码切换、数据请求和UI渲染。使用v-for循环渲染分页按钮,通过计算属性动态生成页码数组:

<template>
  <div>
    <ul>
      <li v-for="item in currentPageData" :key="item.id">{{ item.name }}</li>
    </ul>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <button 
        v-for="page in pageNumbers" 
        :key="page"
        @click="goToPage(page)"
        :class="{ active: currentPage === page }"
      >
        {{ page }}
      </button>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: [],       // 所有数据
      currentPage: 1,    // 当前页码
      pageSize: 10,     // 每页条数
      totalItems: 0     // 总数据量
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.pageSize)
    },
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.allData.slice(start, end)
    },
    pageNumbers() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    },
    goToPage(page) {
      this.currentPage = page
    }
  }
}
</script>

结合API请求实现动态分页

实际项目中通常需要从后端API获取分页数据。使用axios等HTTP库发送请求时,将当前页码和每页大小作为参数:

methods: {
  async fetchData() {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.allData = response.data.items
      this.totalItems = response.data.total
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  goToPage(page) {
    this.currentPage = page
    this.fetchData()
  }
},
created() {
  this.fetchData()
}

优化分页UI体验

对于数据量大的情况,可以改进分页按钮的显示方式,避免展示过多页码。常见做法是显示当前页前后各2页,其余用省略号表示:

computed: {
  pageNumbers() {
    const range = []
    const start = Math.max(1, this.currentPage - 2)
    const end = Math.min(this.totalPages, this.currentPage + 2)

    if (start > 1) range.push(1)
    if (start > 2) range.push('...')

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

    if (end < this.totalPages - 1) range.push('...')
    if (end < this.totalPages) range.push(this.totalPages)

    return range
  }
}

使用第三方分页组件

Vue生态中有许多成熟的分页组件可供选择,如Element UI的Pagination组件:

<template>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="totalItems"
    layout="prev, pager, next">
  </el-pagination>
</template>

<script>
import { ElPagination } from 'element-plus'

export default {
  components: { ElPagination },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>

处理分页查询参数

在需要保持分页状态的路由应用中,可以将分页参数同步到URL查询字符串中:

vue中如何实现分页

watch: {
  currentPage(newVal) {
    this.$router.push({
      query: { ...this.$route.query, page: newVal }
    })
  }
},
created() {
  if (this.$route.query.page) {
    this.currentPage = Number(this.$route.query.page)
  }
  this.fetchData()
}

分享给朋友:

相关文章

vue如何实现id

vue如何实现id

在 Vue 中实现 ID 绑定 Vue 提供了多种方式为元素或组件绑定唯一的 ID,可以根据具体需求选择合适的方法。 方法一:使用 v-bind 绑定静态或动态 ID <template&g…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

分页用vue实现

分页用vue实现

实现分页的基本思路 在Vue中实现分页功能,通常需要以下几个核心部分:数据列表、当前页码、每页显示数量、总页数计算、页码切换逻辑。以下是一个完整的实现方案。 分页组件代码示例 <templat…

vue如何实现vmodel

vue如何实现vmodel

Vue 中实现 v-model 的方法 v-model 是 Vue 中用于实现表单元素和数据双向绑定的指令。其本质是语法糖,结合了 value 属性和 input 事件的封装。以下是实现 v-mode…

vue实现滚动分页

vue实现滚动分页

实现滚动分页的基本思路 滚动分页(Infinite Scroll)是一种常见的前端分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 结合现代前端工具可以轻松实现这一功能。 监听滚动事件…

vue实现分页缩进

vue实现分页缩进

vue实现分页缩进的方法 使用v-for和计算属性实现分页 通过计算属性对数据进行分页处理,结合v-for渲染分页数据。计算属性根据当前页码和每页显示数量对原始数据进行切片。 computed: {…