…">
当前位置:首页 > VUE

vue实现分页页面

2026-02-24 02:08:23VUE

Vue 分页实现方法

基础分页组件实现

创建一个可复用的分页组件 Pagination.vue

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

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

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

<style>
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

在父组件中使用分页

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

    <!-- 分页组件 -->
    <Pagination
      :total-items="data.length"
      :items-per-page="itemsPerPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

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

export default {
  components: { Pagination },
  data() {
    return {
      data: [], // 从API获取的数据
      itemsPerPage: 10,
      currentPage: 1
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.data.slice(start, end)
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
    },
    fetchData() {
      // API请求获取数据
      // this.data = response.data
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

高级分页功能实现

对于大量数据的分页显示,可以添加页码省略功能:

vue实现分页页面

// 在Pagination组件的computed中替换pages计算属性
pages() {
  const range = []
  const current = this.currentPage
  const total = this.totalPages
  const delta = 2 // 显示当前页前后多少页

  range.push(1)
  if (current - delta > 2) {
    range.push('...')
  }

  for (let i = Math.max(2, current - delta); i <= Math.min(total - 1, current + delta); i++) {
    range.push(i)
  }

  if (current + delta < total - 1) {
    range.push('...')
  }
  if (total > 1) {
    range.push(total)
  }

  return range
}

与后端API集成

当需要从后端获取分页数据时:

methods: {
  async fetchData() {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          per_page: this.itemsPerPage
        }
      })
      this.data = response.data.items
      this.totalItems = response.data.total_count
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData()
  }
}

使用第三方库

可以考虑使用现成的分页组件库:

vue实现分页页面

  • v-pagination (轻量级)
  • element-uiel-pagination
  • vuetifyv-pagination

安装示例(使用v-pagination):

npm install v-pagination

使用方式:

<template>
  <pagination
    v-model="currentPage"
    :records="totalItems"
    :per-page="itemsPerPage"
    @paginate="fetchData"
  />
</template>

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

相关文章

vue 实现页面返回

vue 实现页面返回

实现页面返回的方法 在Vue中实现页面返回功能可以通过多种方式实现,以下是几种常见的方案: 使用Vue Router的go方法 通过Vue Router的go方法可以控制浏览器的历史记录导航。ro…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。需要先安装Element UI库。 <t…

vue实现反馈页面

vue实现反馈页面

Vue 实现反馈页面的方法 基础表单结构 使用 Vue 的模板语法构建反馈表单,包含输入框、下拉选择、复选框等基础元素。表单需绑定 v-model 实现数据双向绑定。 <template>…

Vue实现lagger页面

Vue实现lagger页面

Vue实现懒加载页面的方法 懒加载(Lazy Loading)是一种优化技术,用于延迟加载页面或组件,直到它们真正需要显示时才加载。以下是几种在Vue中实现懒加载页面的常见方法: 使用Vue Ro…

vue实现页面分模块

vue实现页面分模块

Vue 实现页面分模块的方法 在 Vue 中实现页面分模块可以通过组件化、路由分割、动态加载等方式实现,以下是具体方法: 组件化拆分 将页面拆分为多个功能独立的组件,每个组件负责特定功能模块。通过…

vue搜索过后实现分页

vue搜索过后实现分页

实现 Vue 搜索后分页的方法 数据绑定与搜索逻辑 在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数: data() { return { originalL…