当前位置:首页 > VUE

vue实现页面段分页

2026-02-20 15:49:41VUE

Vue 实现页面分段分页

在 Vue 中实现分段分页(前端分页)通常需要结合数据截取和分页组件完成。以下是两种常见实现方式:

基于计算属性的前端分页

通过计算属性对数据进行分片处理,适用于数据量较小的情况。

vue实现页面段分页

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

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

使用第三方分页组件

对于复杂场景,推荐使用现成的分页组件库如 element-uiv-pagination

安装依赖

vue实现页面段分页

npm install element-plus

实现代码

<template>
  <el-pagination
    v-model:current-page="currentPage"
    :page-size="itemsPerPage"
    :total="totalItems"
    layout="prev, pager, next"
    @current-change="handlePageChange"
  />
</template>

<script setup>
import { ref } from 'vue';

const currentPage = ref(1);
const itemsPerPage = 10;
const totalItems = ref(100); // 总数据量

const handlePageChange = (page) => {
  // 这里可以触发API请求或更新数据
  fetchData(page);
};
</script>

结合后端API的分页

当数据量较大时,建议采用后端分页:

methods: {
  async fetchData(page) {
    const res = await axios.get('/api/data', {
      params: { page, size: this.itemsPerPage }
    });
    this.dataList = res.data.items;
    this.totalItems = res.data.total;
  }
}

关键点说明

  • 计算属性分页:适合静态数据,通过 slice() 实现内存分页
  • 组件库分页:提供现成的UI和交互逻辑,如 element-ui 的页码跳转
  • 后端分页:大数据场景必选,通过API参数控制分页

根据项目需求选择合适方案,小型应用可用纯前端分页,大型应用建议前后端协同分页。

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

相关文章

如何react页面

如何react页面

创建 React 页面 使用 create-react-app 快速初始化项目: npx create-react-app my-app cd my-app npm start 编写组件代码 在 s…

vue页面实现日历

vue页面实现日历

Vue 页面实现日历的方法 使用第三方组件库 推荐使用成熟的日历组件库,如 v-calendar 或 fullcalendar-vue,快速实现功能丰富的日历。 安装 v-calendar: np…

jquery页面跳转

jquery页面跳转

jQuery 页面跳转方法 使用 jQuery 实现页面跳转可以通过多种方式完成,以下是几种常见的方法: 使用 window.location.href 通过修改 window.location.h…

实现js页面跳转页面

实现js页面跳转页面

使用 window.location.href 通过修改 window.location.href 实现跳转,浏览器会加载新页面并记录到历史记录中: window.location.href = "…

vue登录页面实现

vue登录页面实现

实现Vue登录页面的基本步骤 创建Login.vue文件作为登录组件,包含表单元素(用户名/密码输入框、提交按钮)。使用v-model双向绑定数据,通过@submit.prevent阻止默认表单提交行…

vue实现前端页面

vue实现前端页面

Vue 实现前端页面的核心方法 安装与初始化项目 使用 Vue CLI 快速初始化项目,确保已安装 Node.js 和 npm/yarn: npm install -g @vue/cli vue c…