当前位置:首页 > VUE

前端vue实现分页

2026-01-17 15:15:10VUE

前端Vue实现分页的方法

在Vue中实现分页功能可以通过多种方式完成,以下是常见的实现方法:

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能:

前端vue实现分页

<template>
  <div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50]"
      :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() {
      // 调用API获取数据
    }
  }
}
</script>

自定义分页组件

如果需要完全自定义的分页组件,可以手动实现:

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1">
      上一页
    </button>

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

    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

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

<style>
.pagination {
  display: flex;
  gap: 5px;
}
.active {
  font-weight: bold;
  color: blue;
}
</style>

结合API实现数据分页

实际项目中通常需要与后端API配合实现分页:

前端vue实现分页

methods: {
  async fetchData() {
    try {
      const response = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          pageSize: 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
}

实现无限滚动分页

对于移动端或需要更好用户体验的场景,可以实现无限滚动分页:

mounted() {
  window.addEventListener('scroll', this.handleScroll)
},
destroyed() {
  window.removeEventListener('scroll', this.handleScroll)
},
methods: {
  handleScroll() {
    const bottomOfWindow = 
      document.documentElement.scrollTop + window.innerHeight >= 
      document.documentElement.offsetHeight - 100

    if (bottomOfWindow && !this.loading && this.hasMore) {
      this.currentPage++
      this.fetchData()
    }
  }
}

以上方法可以根据项目需求选择使用或组合使用,实现适合项目的分页功能。

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

相关文章

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…