当前位置:首页 > 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);

在组件中使用:

<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

分享给朋友:

相关文章

h5如何实现vr效果

h5如何实现vr效果

使用WebXR API实现VR效果 WebXR是浏览器中实现VR/AR的核心API,支持设备姿态追踪、渲染交互等功能。需在支持WebXR的设备(如Oculus、HTC Vive)或浏览器模拟环境中运行…

vue如何实现

vue如何实现

Vue 实现方法 Vue 提供了多种方式来实现功能,具体取决于需求。以下是一些常见场景的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。在组件中可以通过 pr…

h5实现左右分页

h5实现左右分页

实现左右分页的HTML5方法 在HTML5中实现左右分页效果可以通过多种方式完成,以下是几种常见的方法: 使用CSS Flexbox布局 Flexbox是一种现代的布局方式,可以轻松实现左右分页效果…

vue如何实现mvvm

vue如何实现mvvm

Vue 的 MVVM 实现原理 Vue 通过数据绑定和响应式系统实现 MVVM(Model-View-ViewModel)模式。其核心在于将数据模型(Model)与视图(View)通过 ViewMod…

react如何实现混入

react如何实现混入

在React中实现混入(Mixin)功能可以通过以下几种方式实现,React官方已不推荐使用传统的React.createClass混入方式,但可通过高阶组件或自定义Hook替代。 高阶组件(HOC…

java如何实现跨平台

java如何实现跨平台

Java实现跨平台的原理 Java通过“一次编写,到处运行”的设计理念实现跨平台能力。其核心机制包括: Java虚拟机(JVM) Java源代码编译为字节码(.class文件),由JVM解释执行。不…