当前位置:首页 > 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 中引入:

如何实现分页vue

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 中引入:

如何实现分页vue

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);

在组件中使用:

<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分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…

vue搜索过后实现分页

vue搜索过后实现分页

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

vue如何实现重新实现主题

vue如何实现重新实现主题

Vue 主题切换的实现方法 使用 CSS 变量动态切换主题 定义主题相关的 CSS 变量,通过修改这些变量实现主题切换。在根元素(如 :root)中定义默认主题的变量,在特定类名下定义其他主题的变量。…

vue如何实现单选

vue如何实现单选

使用原生 HTML 单选按钮 在 Vue 中可以直接使用 HTML 的原生单选按钮,通过 v-model 绑定数据。 <template> <div> <…

前段vue如何实现

前段vue如何实现

Vue 前端实现方法 Vue 是一款流行的前端框架,用于构建用户界面。以下是一些常见的 Vue 实现方法: 组件化开发 Vue 的核心思想是组件化开发。每个组件可以独立封装逻辑、模板和样式,便于复用…

vue如何实现轮询

vue如何实现轮询

实现轮询的方法 在Vue中实现轮询可以通过以下几种方式完成,具体选择取决于项目需求和场景。 使用 setInterval 通过 setInterval 定时调用接口或执行任务,适合简单的轮询场景。…