当前位置:首页 > VUE

vue page 实现分页

2026-02-17 04:45:30VUE

实现分页的基本思路

在Vue中实现分页功能通常需要结合后端API返回的分页数据(如当前页码、每页条数、总数据量等)和前端的分页控件。以下是基于Element UI的分页组件实现方案,其他UI库或自定义分页逻辑类似。

安装Element UI(若未安装)

npm install element-ui

main.js中全局引入:

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

分页组件代码示例

<template>
  <div>
    <!-- 数据表格 -->
    <el-table :data="tableData" border>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
    </el-table>

    <!-- 分页控件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],   // 当前页数据
      currentPage: 1,  // 当前页码
      pageSize: 10,    // 每页条数
      total: 0         // 总数据量
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      // 模拟API请求,实际替换为axios调用
      const mockData = Array.from({ length: 100 }, (_, i) => ({
        id: i + 1,
        name: `项目${i + 1}`
      }));

      // 分页逻辑
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.tableData = mockData.slice(start, end);
      this.total = mockData.length;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    }
  }
};
</script>

与后端API联调

实际项目中通常需要从后端获取分页数据。以下是使用Axios的示例:

methods: {
  async fetchData() {
    try {
      const res = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      });
      this.tableData = res.data.list;
      this.total = res.data.total;
    } catch (error) {
      console.error(error);
    }
  }
}

自定义分页组件

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

vue page 实现分页

<template>
  <div class="pagination">
    <button 
      v-for="page in pageCount" 
      :key="page"
      @click="goToPage(page)"
      :class="{ active: currentPage === page }">
      {{ page }}
    </button>
  </div>
</template>

<script>
export default {
  props: ['total', 'perPage'],
  data() {
    return { currentPage: 1 };
  },
  computed: {
    pageCount() {
      return Math.ceil(this.total / this.perPage);
    }
  },
  methods: {
    goToPage(page) {
      this.currentPage = page;
      this.$emit('page-changed', page);
    }
  }
};
</script>

<style>
.pagination button { margin: 0 5px; }
.pagination button.active { font-weight: bold; }
</style>

关键注意事项

  • 分页参数需与后端约定一致(通常为page/sizepageNum/pageSize
  • 处理无数据时的空状态显示
  • 大型数据集建议使用服务器端分页而非前端分页
  • 考虑添加加载状态提示(如loading动画)

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

相关文章

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…