当前位置:首页 > 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参数控制分页

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

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

相关文章

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://example…

h5实现登录页面跳转页面跳转页面跳转

h5实现登录页面跳转页面跳转页面跳转

使用HTML5实现登录页面跳转 在HTML5中实现登录页面跳转可以通过多种方式完成,包括表单提交、JavaScript跳转和超链接跳转。以下是几种常见的实现方法。 表单提交跳转 使用HTML表单的a…

vue实现页面定位

vue实现页面定位

Vue 实现页面定位的方法 使用 scrollIntoView 方法 通过 JavaScript 的 scrollIntoView 方法可以实现平滑滚动到指定元素位置。在 Vue 中,可以通过 ref…

h5页面滑动效果实现

h5页面滑动效果实现

实现H5页面滑动效果的方法 H5页面滑动效果可以通过多种方式实现,包括原生JavaScript、CSS动画或第三方库。以下是几种常见的方法: 使用CSS Scroll Snap CSS Scroll…

vue实现页面转换

vue实现页面转换

Vue 实现页面转换的方法 Vue 提供了多种方式实现页面转换,主要包括路由切换动画和组件过渡效果。以下是几种常见的实现方法: 使用 Vue Router 和过渡动画 通过 Vue 的 <tr…

vue实现页面缩略

vue实现页面缩略

Vue实现页面缩略的方法 使用CSS的transform: scale() 通过CSS的transform属性可以实现页面缩放效果。在Vue组件中,可以动态绑定样式来控制缩放比例。 <temp…