当前位置:首页 > VUE

vue中分页怎么实现

2026-01-21 18:10:23VUE

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,适用于Vue 2项目。安装Element UI后,直接引入分页组件并绑定数据即可。

<template>
  <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>
</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>

使用Ant Design Vue的分页组件

Ant Design Vue的a-pagination组件适用于Vue 3项目,功能与Element UI类似。

<template>
  <a-pagination
    v-model:current="currentPage"
    v-model:pageSize="pageSize"
    :total="total"
    show-size-changer
    @change="handlePageChange"
  />
</template>

<script>
import { ref } from 'vue';
export default {
  setup() {
    const currentPage = ref(1);
    const pageSize = ref(10);
    const total = ref(100);

    const handlePageChange = () => {
      // 根据currentPage和pageSize请求数据
    };

    return { currentPage, pageSize, total, handlePageChange };
  }
}
</script>

手动实现分页逻辑

如果不依赖UI库,可以通过计算属性手动实现分页逻辑。假设有一个数据数组listData,需要按每页pageSize条数据进行分割。

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage">上一页</button>
    <span>当前页: {{ currentPage }}</span>
    <button @click="nextPage">下一页</button>
  </div>
</template>

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

结合后端API实现分页

实际项目中,分页通常需要与后端API交互。通过传递pagepageSize参数获取对应数据。

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

分页样式自定义

如果需要自定义分页样式,可以通过覆盖UI库的CSS或手动编写样式。例如修改Element UI分页按钮颜色:

vue中分页怎么实现

.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #ff6b6b;
}

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

相关文章

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现select

vue实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML select 元素 原生 HTML 的 <sel…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue 实现文字滚动

vue 实现文字滚动

Vue 实现文字滚动效果 使用CSS动画实现 通过CSS的@keyframes和transform属性实现文字滚动效果: <template> <div class="scro…

原生实现vue功能

原生实现vue功能

原生JavaScript实现Vue核心功能 使用原生JavaScript模拟Vue的核心功能需要理解响应式数据绑定、虚拟DOM和组件系统等概念。以下是关键实现方法: 响应式数据系统 通过Object…