当前位置:首页 > VUE

vue实现分页效果

2026-01-19 16:35:16VUE

使用Element UI实现分页

安装Element UI库后,可以直接使用el-pagination组件。需要在Vue项目中引入并注册Element UI。

<template>
  <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>
</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() {
      // 获取分页数据逻辑
    }
  }
}
</script>

自定义分页组件实现

如果需要完全自定义分页组件,可以创建一个独立的Pagination组件。

<template>
  <div class="pagination">
    <button 
      :disabled="currentPage === 1" 
      @click="changePage(currentPage - 1)">
      上一页
    </button>

    <span v-for="page in pages" :key="page">
      <button 
        @click="changePage(page)"
        :class="{ active: currentPage === page }">
        {{ page }}
      </button>
    </span>

    <button 
      :disabled="currentPage === totalPages" 
      @click="changePage(currentPage + 1)">
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pages() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page)
      }
    }
  }
}
</script>

<style>
.pagination button {
  margin: 0 5px;
}
.pagination button.active {
  background-color: #409EFF;
  color: white;
}
</style>

与后端API交互

实现分页通常需要与后端API配合,传递分页参数并接收分页数据。

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

使用Vuex管理分页状态

在大型应用中,可以使用Vuex集中管理分页状态。

// store/modules/pagination.js
const state = {
  currentPage: 1,
  pageSize: 10,
  total: 0
}

const mutations = {
  SET_PAGE(state, page) {
    state.currentPage = page
  },
  SET_PAGE_SIZE(state, size) {
    state.pageSize = size
  },
  SET_TOTAL(state, total) {
    state.total = total
  }
}

const actions = {
  updatePage({ commit }, page) {
    commit('SET_PAGE', page)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

响应式分页布局

可以根据屏幕尺寸调整分页显示方式,提升移动端体验。

vue实现分页效果

<template>
  <div class="pagination-container">
    <div class="pagination-buttons" v-if="!isMobile">
      <!-- 桌面端完整分页 -->
    </div>
    <div class="mobile-pagination" v-else>
      <!-- 移动端简化分页 -->
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    isMobile() {
      return window.innerWidth < 768
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.$forceUpdate()
    }
  }
}
</script>

标签: 分页效果
分享给朋友:

相关文章

分页用vue实现

分页用vue实现

分页用 Vue 实现 在 Vue 中实现分页功能通常需要结合数据管理和 UI 组件。以下是几种常见的方法: 使用 Vue 组件库的分页组件 许多 Vue UI 组件库(如 Element UI、Va…

react 如何分页

react 如何分页

分页实现方法 在React中实现分页功能可以通过多种方式完成,具体取决于数据来源(如API或本地数据)和UI库的选择。以下是常见的实现方法: 使用本地数据分页 对于存储在组件状态或Context中…

vue实现过渡效果

vue实现过渡效果

Vue 过渡效果实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于在元素或组件进入/离开 DOM 时应用过渡效果。以…

vue实现效果更新

vue实现效果更新

Vue 实现效果更新的方法 Vue 提供了多种方式实现动态效果更新,主要依赖其响应式系统和生命周期钩子。以下是几种常见的方法: 数据绑定与响应式更新 Vue 的核心是数据驱动视图。通过修改数据,视图…

vue实现前端分页

vue实现前端分页

实现前端分页的方法 在Vue中实现前端分页通常需要结合数据截取和分页组件。以下是常见的实现方式: 基础实现方案 数据准备 定义总数据数组和分页相关变量: data() { return {…

vue 实现toogle效果

vue 实现toogle效果

使用 v-if 和 v-else 实现切换 通过 Vue 的指令 v-if 和 v-else 可以轻松实现元素的显示与隐藏切换。 <template> <button @c…