当前位置:首页 > VUE

vue实现换页

2026-03-27 20:53:58VUE

Vue实现分页功能

使用Vue实现分页功能通常涉及前端分页逻辑和后端API配合。以下是两种常见实现方式:

前端分页实现

适用于数据量较小的情况,所有数据一次性加载到前端:

<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 {
      currentPage: 1,
      itemsPerPage: 10,
      allItems: [] // 全部数据
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.allItems.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.allItems.length / this.itemsPerPage)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

后端分页实现

适用于大数据量场景,通过API分页请求:

<template>
  <!-- 同上 -->
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      paginatedItems: [],
      totalItems: 0
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    }
  },
  methods: {
    async fetchData() {
      const res = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          limit: this.itemsPerPage
        }
      })
      this.paginatedItems = res.data.items
      this.totalItems = res.data.total
    },
    nextPage() {
      this.currentPage++
      this.fetchData()
    },
    prevPage() {
      this.currentPage--
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

使用第三方分页组件

Element UI分页组件示例:

<template>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total"
    layout="prev, pager, next">
  </el-pagination>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>

分页样式优化

可以添加跳转页码输入框:

vue实现换页

<input v-model="goToPage" type="number" min="1" :max="totalPages">
<button @click="goToPageHandler">跳转</button>

methods: {
  goToPageHandler() {
    if (this.goToPage >= 1 && this.goToPage <= this.totalPages) {
      this.currentPage = Number(this.goToPage)
      this.fetchData()
    }
  }
}

以上实现可根据具体需求调整分页逻辑和UI样式。后端分页需要配合后端接口实现,通常接口需要支持page和limit参数。

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

相关文章

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化 使…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…