当前位置:首页 > 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>

手动实现分页逻辑

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

<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 实现分页

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

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

无限滚动分页

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

vue加载实现分页

<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 router实现分页

vue router实现分页

使用 Vue Router 实现分页 在 Vue.js 项目中,可以通过 Vue Router 结合分页组件实现分页功能。以下是实现分页的几种方法: 使用路由参数传递页码 在路由配置中定义一个动态参…

jquery分页

jquery分页

jQuery分页的实现方法 jQuery分页可以通过多种方式实现,以下是几种常见的方法: 使用插件实现分页 jQuery有许多优秀的分页插件,如jQuery Pagination、DataTabl…

vue前端实现分页

vue前端实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。核心逻辑包括计算总页数、处理当前页码变化以及动态加载数据。 使用Element UI的分页组件 Element…

react如何实现分页

react如何实现分页

实现分页的基本思路 在React中实现分页通常需要以下几个核心步骤:管理当前页码状态、计算分页数据、渲染分页控件。以下是一个典型的分页实现方法。 管理分页状态 使用React的useState钩子来…

vue实现无线加载

vue实现无线加载

无限滚动加载的实现 在Vue中实现无限滚动加载(Infinite Scroll)可以通过监听滚动事件或使用现成的库完成。以下是两种常见方法: 使用原生滚动事件监听 通过监听滚动事件判断是否到…

vue实现 懒加载

vue实现 懒加载

懒加载的实现方式 在Vue中实现懒加载可以通过多种方式,常见的包括路由懒加载和组件懒加载。以下是具体实现方法: 路由懒加载 路由懒加载是通过动态导入组件的方式实现的,适用于Vue Router的路由…