当前位置:首页 > VUE

vue怎么实现分页查询

2026-02-20 18:31:56VUE

Vue 实现分页查询的方法

前端分页实现

使用 v-for 渲染分页数据,结合计算属性实现前端分页:

data() {
  return {
    currentPage: 1,
    pageSize: 10,
    totalItems: 100,
    allItems: [] // 所有数据
  }
},
computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.pageSize
    const end = start + this.pageSize
    return this.allItems.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.totalItems / this.pageSize)
  }
}

后端分页实现

通过 API 请求获取分页数据:

methods: {
  fetchData() {
    const params = {
      page: this.currentPage,
      size: this.pageSize
    }
    axios.get('/api/data', { params })
      .then(response => {
        this.paginatedItems = response.data.items
        this.totalItems = response.data.total
      })
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData()
  }
}

分页组件使用

推荐使用 Element UI 的分页组件:

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

自定义分页组件

实现简单分页控件:

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

分页优化技巧

添加 loading 状态提升用户体验:

data() {
  return {
    isLoading: false
  }
},
methods: {
  async fetchData() {
    this.isLoading = true
    try {
      // API 调用
    } finally {
      this.isLoading = false
    }
  }
}

使用防抖减少频繁请求:

vue怎么实现分页查询

import { debounce } from 'lodash'

methods: {
  handlePageChange: debounce(function(page) {
    this.currentPage = page
    this.fetchData()
  }, 300)
}

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

相关文章

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…

vue实现城市

vue实现城市

Vue 实现城市选择功能 使用 Element UI 的 Cascader 组件 Element UI 提供了一个 Cascader 级联选择器组件,非常适合实现城市选择功能。需要先安装 Elemen…

vue实现拉伸

vue实现拉伸

Vue 实现元素拉伸功能 在Vue中实现元素的拉伸(拖拽调整大小)功能,可以通过监听鼠标事件结合CSS样式来实现。以下是两种常见实现方式: 使用原生事件监听 创建可拉伸的组件需要处理鼠标按下、移动和…