当前位置:首页 > VUE

vue加载实现分页

2026-03-08 15:37:43VUE

Vue 实现分页加载的方法

使用第三方库(如 Element UI)

Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页功能。安装 Element UI 后,在组件中引入并使用分页组件。

<template>
  <div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total">
    </el-pagination>
  </div>
</template>

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

手动实现分页逻辑

vue加载实现分页

如果不使用第三方库,可以手动实现分页逻辑。通过计算属性或方法对数据进行分页处理。

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

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

结合后端 API 实现分页

vue加载实现分页

通常分页数据由后端提供,前端只需传递当前页码和每页大小。后端返回对应的数据及总条数。

<template>
  <div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total">
    </el-pagination>
  </div>
</template>

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

无限滚动分页

对于移动端或需要流畅体验的场景,可以使用无限滚动分页。监听滚动事件,滚动到底部时加载更多数据。

<template>
  <div ref="scrollContainer" @scroll="handleScroll">
    <div v-for="item in data" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [],
      currentPage: 1,
      pageSize: 10,
      isLoading: false,
      hasMore: true
    }
  },
  methods: {
    handleScroll() {
      const container = this.$refs.scrollContainer
      if (container.scrollTop + container.clientHeight >= container.scrollHeight - 100 && !this.isLoading && this.hasMore) {
        this.loadMore()
      }
    },
    async loadMore() {
      this.isLoading = true
      const res = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.data = [...this.data, ...res.data.list]
      this.currentPage++
      this.hasMore = res.data.list.length === this.pageSize
      this.isLoading = false
    }
  },
  created() {
    this.loadMore()
  }
}
</script>

标签: 分页加载
分享给朋友:

相关文章

vue加载动画实现

vue加载动画实现

Vue 加载动画的实现方法 在 Vue 中实现加载动画可以通过多种方式完成,以下是一些常见的方法: 使用 CSS 动画 通过 CSS 定义动画效果,结合 Vue 的动态类绑定或条件渲染控制动画的显示…

vue懒加载实现难吗

vue懒加载实现难吗

Vue懒加载的实现难度 Vue懒加载的实现并不复杂,尤其是借助现代工具和库的支持。懒加载的核心思想是按需加载资源,通常用于路由或组件,以提升页面初始加载速度。以下是常见的实现方法和注意事项:…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

vue分页实现原理

vue分页实现原理

Vue 分页实现原理 Vue 分页的核心原理是通过计算当前页的数据范围,动态渲染数据列表并控制分页组件的交互。以下是关键实现步骤: 数据分片计算 分页需要根据当前页码和每页条数截取数据。假设原始数据…

vue实现分页组件

vue实现分页组件

Vue 分页组件实现 基础分页组件结构 创建一个名为 Pagination.vue 的文件,包含以下基础模板和逻辑: <template> <div class="pa…

vue实现滚动分页

vue实现滚动分页

实现滚动分页的基本思路 滚动分页(Infinite Scroll)是一种常见的前端分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 结合现代前端工具可以轻松实现这一功能。 监听滚动事件…