当前位置:首页 > VUE

vue中实现分页

2026-03-10 01:13:07VUE

vue中实现分页的方法

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。安装Element UI后,在组件中引入并使用。

<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() {
      // 根据currentPage和pageSize获取数据
    }
  }
}
</script>

自定义分页组件

如果需要更灵活的分页逻辑,可以手动实现分页组件。通过计算属性对数据进行分页处理。

<template>
  <div>
    <div v-for="item in paginatedData" :key="item.id">
      {{ item.name }}
    </div>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

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

结合后端API实现分页

实际项目中,分页通常与后端API配合使用。前端传递页码和每页条数,后端返回对应数据。

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

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      list: []
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      axios.get('/api/list', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      }).then(res => {
        this.list = res.data.list;
        this.total = res.data.total;
      });
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.getList();
    }
  }
}
</script>

使用第三方库vue-awesome-paginate

如果需要更多功能,可以使用专门的分页库如vue-awesome-paginate。安装后引入并使用。

vue中实现分页

<template>
  <div>
    <vue-awesome-paginate
      :total-items="total"
      :items-per-page="perPage"
      :max-pages-shown="5"
      v-model="currentPage"
      :on-click="onClickHandler"
    />
  </div>
</template>

<script>
import { VueAwesomePaginate } from 'vue-awesome-paginate';

export default {
  components: {
    VueAwesomePaginate
  },
  data() {
    return {
      currentPage: 1,
      perPage: 10,
      total: 100
    }
  },
  methods: {
    onClickHandler(page) {
      this.currentPage = page;
      this.fetchData();
    }
  }
}
</script>

以上方法可以根据项目需求选择适合的分页实现方式。

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

相关文章

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…