当前位置:首页 > VUE

vue实现分页加载数据

2026-01-20 21:19:27VUE

实现分页加载数据的方法

在Vue中实现分页加载数据,通常需要结合后端API和前端分页组件。以下是几种常见的实现方式:

使用Element UI的Pagination组件

安装Element UI后,可以直接使用其分页组件:

<template>
  <div>
    <el-table :data="tableData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    fetchData() {
      // 调用API获取数据
      api.getList({
        page: this.currentPage,
        size: this.pageSize
      }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

使用自定义分页组件

如果不使用UI库,可以创建自定义分页组件:

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: Number,
    totalPages: Number
  },
  methods: {
    prevPage() {
      this.$emit('page-change', this.currentPage - 1)
    },
    nextPage() {
      this.$emit('page-change', this.currentPage + 1)
    }
  }
}
</script>

无限滚动分页

对于移动端或需要无限滚动的场景,可以使用Intersection Observer API:

<template>
  <div>
    <div v-for="item in items" :key="item.id">
      <!-- 渲染项目 -->
    </div>
    <div ref="loader" v-if="!isLastPage">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      isLastPage: false,
      isLoading: false
    }
  },
  mounted() {
    this.initObserver()
    this.loadItems()
  },
  methods: {
    initObserver() {
      const observer = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && !this.isLoading) {
          this.page++
          this.loadItems()
        }
      })
      observer.observe(this.$refs.loader)
    },
    loadItems() {
      this.isLoading = true
      api.getItems(this.page).then(res => {
        this.items = [...this.items, ...res.data.items]
        this.isLastPage = res.data.isLastPage
        this.isLoading = false
      })
    }
  }
}
</script>

使用Vuex管理分页状态

对于复杂应用,可以使用Vuex集中管理分页状态:

vue实现分页加载数据

// store/modules/list.js
const state = {
  items: [],
  pagination: {
    page: 1,
    size: 10,
    total: 0
  }
}

const actions = {
  async fetchItems({ commit }, params) {
    const res = await api.getItems(params)
    commit('SET_ITEMS', res.data.items)
    commit('SET_PAGINATION', {
      page: params.page,
      total: res.data.total
    })
  }
}

const mutations = {
  SET_ITEMS(state, items) {
    state.items = items
  },
  SET_PAGINATION(state, pagination) {
    state.pagination = { ...state.pagination, ...pagination }
  }
}

以上方法可以根据具体需求选择使用,Element UI的分页组件适合快速开发,自定义分页组件提供更多灵活性,无限滚动适合移动端体验,Vuex则适合状态管理复杂的大型应用。

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

相关文章

vue实现懒加载的方法

vue实现懒加载的方法

Vue 实现懒加载的方法 路由懒加载 通过动态导入语法拆分路由组件,减少初始加载时间。适用于 Vue Router 配置: const routes = [ { path: '/home…

vue实现选项卡分页

vue实现选项卡分页

实现选项卡分页的基本思路 在Vue中实现选项卡分页功能,可以通过动态组件或条件渲染结合数据绑定完成。核心逻辑是维护一个当前激活的选项卡状态,根据用户点击切换内容。 使用v-if条件渲染 通过v-if…

vue分页实现

vue分页实现

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

vue实现后台分页

vue实现后台分页

Vue 实现后台分页的方法 分页组件与数据绑定 在 Vue 中实现后台分页通常需要结合分页组件和后端 API。使用 el-pagination(Element UI)或自定义分页组件,通过 v-mod…

react如何实现分页

react如何实现分页

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

vue实现列表分页

vue实现列表分页

Vue 列表分页实现方法 基础分页实现 安装依赖(如使用第三方库) npm install vue-paginate 模板部分示例 <template> <div>…