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

使用防抖减少频繁请求:

import { debounce } from 'lodash'

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

vue怎么实现分页查询

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

相关文章

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <templat…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutat…