当前位置:首页 > VUE

vue page 实现分页

2026-01-16 03:31:45VUE

实现 Vue 分页功能的方法

使用 Element UI 的分页组件

安装 Element UI 后,可以直接使用 el-pagination 组件实现分页功能。

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

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    fetchData() {
      // 调用 API 获取数据
      console.log(`当前页: ${this.currentPage}, 每页显示: ${this.pageSize}`);
    }
  }
}
</script>

使用自定义分页组件

如果需要完全自定义分页组件,可以手动实现分页逻辑。

vue page 实现分页

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1">
      上一页
    </button>
    <span v-for="page in pages" :key="page">
      <button 
        @click="goToPage(page)" 
        :class="{ active: currentPage === page }">
        {{ page }}
      </button>
    </span>
    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number
  },
  data() {
    return {
      currentPage: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage);
    },
    pages() {
      const pages = [];
      for (let i = 1; i <= this.totalPages; i++) {
        pages.push(i);
      }
      return pages;
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.$emit('page-changed', this.currentPage);
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
        this.$emit('page-changed', this.currentPage);
      }
    },
    goToPage(page) {
      this.currentPage = page;
      this.$emit('page-changed', this.currentPage);
    }
  }
}
</script>

<style>
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

结合 API 实现数据分页

通常分页需要与后端 API 配合,以下是一个结合 axios 的示例。

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

使用 Vue Paginate 插件

Vue Paginate 是一个轻量级的分页插件,适合简单场景。

vue page 实现分页

安装插件:

npm install vue-paginate

使用示例:

import VuePaginate from 'vue-paginate';
Vue.use(VuePaginate);
<template>
  <div>
    <paginate
      :page-count="totalPages"
      :click-handler="goToPage"
      :prev-text="'Prev'"
      :next-text="'Next'"
      :container-class="'pagination'">
    </paginate>
  </div>
</template>

以上方法涵盖了从 UI 组件库到自定义实现的不同分页方案,可以根据项目需求选择合适的方式。

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

相关文章

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template>…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…