当前位置:首页 > VUE

vue 实现翻页

2026-03-07 07:41:21VUE

Vue 实现翻页功能

在 Vue 中实现翻页功能通常需要结合分页组件和数据处理逻辑。以下是几种常见的实现方式:

使用 Element UI 的分页组件

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

vue 实现翻页

<template>
  <div>
    <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 {
      currentPage: 1,
      pageSize: 10,
      total: 100
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      // 根据currentPage和pageSize获取数据
    }
  }
}
</script>

自定义分页组件

如果需要自定义分页组件,可以这样实现:

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

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number
  },
  data() {
    return {
      currentPage: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.$emit('page-changed', this.currentPage)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.$emit('page-changed', this.currentPage)
      }
    }
  }
}
</script>

结合 API 分页请求

实际项目中通常需要从 API 获取分页数据:

vue 实现翻页

methods: {
  async fetchData() {
    try {
      const response = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.dataList = response.data.items
      this.total = response.data.total
    } catch (error) {
      console.error(error)
    }
  }
}

前端本地分页处理

如果数据量不大,可以在前端进行分页处理:

computed: {
  paginatedData() {
    const start = (this.currentPage - 1) * this.pageSize
    const end = start + this.pageSize
    return this.allData.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.allData.length / this.pageSize)
  }
}

样式优化

可以为分页组件添加基本样式:

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 20px 0;
}

.pagination button {
  margin: 0 10px;
  padding: 5px 10px;
  cursor: pointer;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

以上方法可以根据项目需求选择使用,Element UI 的分页组件适合快速开发,自定义组件则提供更多灵活性。实际应用中通常需要结合 API 请求实现完整的分页功能。

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

相关文章

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios 获…

vue插槽实现

vue插槽实现

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

vue 实现弹窗

vue 实现弹窗

Vue 实现弹窗的方法 在 Vue 中实现弹窗功能可以通过多种方式完成,以下是几种常见的方法: 使用组件和 v-if/v-show 控制显示 创建一个独立的弹窗组件,通过 v-if 或 v-show…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…

用vue实现echarts

用vue实现echarts

使用 Vue 实现 ECharts 安装依赖 在 Vue 项目中安装 ECharts 和 Vue-ECharts(官方推荐的 Vue 封装库): npm install echarts vue-ec…

vue如何实现mvvm

vue如何实现mvvm

Vue 实现 MVVM 的核心机制 Vue 的 MVVM(Model-View-ViewModel)实现依赖于数据绑定和响应式系统,通过以下核心机制完成: 数据劫持(响应式系统) Vue 使用 Ob…