当前位置:首页 > VUE

vue的分页怎么实现

2026-01-20 16:53:45VUE

实现 Vue 分页的常见方法

使用第三方库(如 element-uivant

安装 element-ui 分页组件:

npm install element-ui

在组件中引入并使用:

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

自定义分页组件

vue的分页怎么实现

创建一个简单的分页组件:

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1">
      上一页
    </button>
    <span>第 {{ currentPage }} 页</span>
    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage);
    }
  },
  methods: {
    prevPage() {
      this.$emit('page-change', this.currentPage - 1);
    },
    nextPage() {
      this.$emit('page-change', this.currentPage + 1);
    }
  }
}
</script>

结合后端 API 实现分页

在 Vue 组件中调用分页接口:

vue的分页怎么实现

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

使用计算属性实现前端分页

适用于数据量较小的情况:

<script>
export default {
  data() {
    return {
      allData: [], // 所有数据
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.allData.slice(start, end);
    },
    totalPages() {
      return Math.ceil(this.allData.length / this.pageSize);
    }
  }
}
</script>

分页样式优化建议

添加 CSS 样式美化分页组件:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  cursor: pointer;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

分页功能扩展建议

  • 添加页码跳转输入框
  • 支持显示页码范围(如 1-10 条,共 100 条)
  • 添加每页条数选择器
  • 实现无刷新分页(AJAX 请求)
  • 添加加载状态指示器

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

相关文章

vue实现点击样式

vue实现点击样式

Vue 实现点击样式的方法 在 Vue 中实现点击样式可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-bind:class 动态绑定类名 通过数据驱动的方式动态切换类名,结合 CS…

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例: &…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vu…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tab…