当前位置:首页 > VUE

前端vue实现分页

2026-03-09 20:49:00VUE

Vue实现分页的方法

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,适合快速集成。

安装Element UI:

npm install element-ui

在Vue文件中引入并使用:

<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() {
      // 根据currentPage和pageSize请求数据
    }
  }
}
</script>

自定义分页组件

如果需要完全自定义分页逻辑,可以手动实现:

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1">
      上一页
    </button>
    <span v-for="page in pages" :key="page">
      <button 
        @click="goToPage(page)" 
        :class="{ active: currentPage === page }">
        {{ page }}
      </button>
    </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);
    },
    pages() {
      const range = [];
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i);
      }
      return range;
    }
  },
  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);
      }
    },
    goToPage(page) {
      this.currentPage = page;
      this.$emit('page-changed', this.currentPage);
    }
  }
}
</script>

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

结合后端API实现

分页通常需要与后端API配合,通过limitoffset参数控制:

<script>
export default {
  methods: {
    async fetchData() {
      const response = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          limit: this.pageSize
        }
      });
      this.items = response.data.items;
      this.total = response.data.total;
    }
  }
}
</script>

使用Vuex管理分页状态

对于大型应用,可以通过Vuex集中管理分页状态:

// store/modules/pagination.js
export default {
  state: {
    currentPage: 1,
    pageSize: 10
  },
  mutations: {
    SET_PAGE(state, page) {
      state.currentPage = page;
    },
    SET_PAGE_SIZE(state, size) {
      state.pageSize = size;
    }
  }
}

在组件中通过mapStatemapMutations使用:

前端vue实现分页

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState('pagination', ['currentPage', 'pageSize'])
  },
  methods: {
    ...mapMutations('pagination', ['SET_PAGE', 'SET_PAGE_SIZE']),
    handlePageChange(page) {
      this.SET_PAGE(page);
      this.fetchData();
    }
  }
}
</script>

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

相关文章

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue 2…