当前位置:首页 > VUE

如何实现分页vue

2026-02-18 19:26:07VUE

使用 vue-paginate 库实现分页

安装 vue-paginate

npm install vue-paginate

main.js 中注册插件:

import Vue from 'vue';
import VuePaginate from 'vue-paginate';

Vue.use(VuePaginate);

在组件中使用:

<template>
  <div>
    <paginate
      v-model="currentPage"
      :page-count="totalPages"
      :click-handler="changePage"
      :prev-text="'Prev'"
      :next-text="'Next'"
      :container-class="'pagination'"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      totalPages: 10,
      items: [] // 分页数据
    };
  },
  methods: {
    changePage(pageNum) {
      this.currentPage = pageNum;
      // 加载对应页数据
    }
  }
};
</script>

<style>
.pagination {
  display: flex;
  list-style: none;
}
.pagination li {
  margin: 0 5px;
  cursor: pointer;
}
</style>

手动实现分页逻辑

计算分页数据:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
    <span>Page {{ currentPage }} of {{ totalPages }}</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [], // 所有数据
      currentPage: 1,
      itemsPerPage: 10
    };
  },
  computed: {
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage);
    },
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.items.slice(start, end);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--;
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++;
    }
  }
};
</script>

使用 Element UI 分页组件

安装 Element UI

npm install element-ui

main.js 中引入:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

在组件中使用:

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

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalItems: 100
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      // 加载对应页数据
    }
  }
};
</script>

使用 Vuetify 分页组件

安装 Vuetify

npm install vuetify

main.js 中引入:

import Vue from 'vue';
import Vuetify from 'vuetify';
import 'vuetify/dist/vuetify.min.css';

Vue.use(Vuetify);

在组件中使用:

<template>
  <v-pagination
    v-model="currentPage"
    :length="totalPages"
    @input="changePage"
  />
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      totalPages: 10
    };
  },
  methods: {
    changePage(page) {
      this.currentPage = page;
      // 加载对应页数据
    }
  }
};
</script>

使用 Bootstrap-Vue 分页组件

安装 Bootstrap-Vue

npm install bootstrap-vue

main.js 中引入:

import Vue from 'vue';
import BootstrapVue from 'bootstrap-vue';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap-vue/dist/bootstrap-vue.css';

Vue.use(BootstrapVue);

在组件中使用:

如何实现分页vue

<template>
  <b-pagination
    v-model="currentPage"
    :total-rows="totalItems"
    :per-page="itemsPerPage"
    @change="changePage"
  />
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      totalItems: 100,
      itemsPerPage: 10
    };
  },
  methods: {
    changePage(page) {
      this.currentPage = page;
      // 加载对应页数据
    }
  }
};
</script>

根据项目需求选择合适的方案,手动实现适合轻量级需求,而库或框架组件可提供更丰富的功能。

分享给朋友:

相关文章

vue手写签名如何实现

vue手写签名如何实现

实现 Vue 手写签名的步骤 使用 canvas 实现基础签名功能 在 Vue 项目中创建一个组件,利用 HTML5 的 canvas 元素实现手写签名功能。通过监听鼠标或触摸事件来捕获用户的绘制路径…

java如何实现多态

java如何实现多态

多态的概念 多态是面向对象编程的三大特性之一(封装、继承、多态),指同一操作作用于不同对象时,可以产生不同的行为。Java中主要通过方法重写(Override)和接口/抽象类实现多态。 实现多态的三…

vue如何实现select

vue如何实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过原生 HTML <select> 元素或使用第三方 UI 库(如 Element UI、Ant Des…

vue page 实现分页

vue page 实现分页

实现 Vue 分页功能的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用 el-pagination 组件实现分页功能。 <template>…

vue如何实现confirm

vue如何实现confirm

Vue 实现 Confirm 弹窗的方法 在 Vue 中实现确认弹窗(Confirm)可以通过多种方式,以下是几种常见的方法: 使用浏览器原生 confirm 最简单的实现方式是直接调用浏览器的原生…

VUE如何实现长按

VUE如何实现长按

VUE 实现长按功能的方法 在Vue中实现长按功能可以通过监听触摸或鼠标事件来实现。以下是几种常见的方法: 方法一:使用原生事件监听 通过监听 touchstart 和 touchend 或 mo…