当前位置:首页 > VUE

vue 分页 实现

2026-01-08 00:33:20VUE

Vue 分页实现方法

使用第三方库(如 Element UI)

Element UI 提供了现成的分页组件 el-pagination,适合快速集成。

安装 Element UI:

npm install element-ui

在 Vue 文件中引入并使用:

<template>
  <div>
    <el-pagination
      @current-change="handleCurrentChange"
      :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: 100
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      // 加载对应页的数据
    }
  }
};
</script>

自定义分页组件

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

vue 分页 实现

定义分页组件:

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

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

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

分页数据逻辑

在父组件中处理分页数据请求或过滤:

vue 分页 实现

<template>
  <div>
    <custom-pagination
      :current-page="currentPage"
      :total-items="items.length"
      :items-per-page="itemsPerPage"
      @page-changed="onPageChanged">
    </custom-pagination>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
import CustomPagination from './CustomPagination.vue';

export default {
  components: { CustomPagination },
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 5,
      items: [
        { id: 1, name: 'Item 1' },
        // 更多数据...
      ]
    };
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.items.slice(start, end);
    }
  },
  methods: {
    onPageChanged(page) {
      this.currentPage = page;
    }
  }
};
</script>

后端分页 API 集成

如果数据来自后端 API,通常需要传递分页参数并处理返回结果。

示例 API 请求:

methods: {
  fetchData(page) {
    axios.get('/api/items', {
      params: {
        page: page,
        per_page: this.itemsPerPage
      }
    }).then(response => {
      this.items = response.data.items;
      this.total = response.data.total;
    });
  }
}

调用时机:

onPageChanged(page) {
  this.currentPage = page;
  this.fetchData(page);
}

通过以上方法,可以实现 Vue 中的分页功能,无论是前端分页还是后端分页均可灵活处理。

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

相关文章

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router…

vue app 实现

vue app 实现

Vue App 实现步骤 创建Vue项目 使用Vue CLI或Vite初始化项目。Vue CLI适合传统项目,Vite适合现代轻量级应用。 npm create vue@latest my-v…