当前位置:首页 > VUE

vue page 实现分页

2026-03-08 16:53:22VUE

实现分页功能的方法

在Vue中实现分页功能通常需要结合后端API和前端组件。以下是几种常见的实现方式:

使用Element UI的分页组件

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

vue page 实现分页

<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() {
      // 调用API获取数据,传递page和size参数
    }
  }
}
</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() {
      if (this.currentPage > 1) {
        this.$emit('page-changed', this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.$emit('page-changed', this.currentPage + 1)
      }
    },
    goToPage(page) {
      this.$emit('page-changed', page)
    }
  }
}
</script>

<style>
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
}
.pagination span {
  margin: 0 5px;
  cursor: pointer;
}
.pagination span.active {
  font-weight: bold;
  color: #409EFF;
}
</style>

结合Vuex管理分页状态

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

vue page 实现分页

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

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

const actions = {
  changePage({ commit }, page) {
    commit('SET_PAGE', page)
  },
  changePageSize({ commit }, size) {
    commit('SET_PAGE_SIZE', size)
  },
  updateTotalItems({ commit }, total) {
    commit('SET_TOTAL_ITEMS', total)
  }
}

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

与后端API交互

实现分页通常需要与后端API配合,以下是一个典型的API调用示例:

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

响应式分页控制

可以根据屏幕尺寸动态调整每页显示的数量:

data() {
  return {
    pageSize: window.innerWidth < 768 ? 5 : 10
  }
},
created() {
  window.addEventListener('resize', this.handleResize)
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize)
},
methods: {
  handleResize() {
    this.pageSize = window.innerWidth < 768 ? 5 : 10
    this.fetchData()
  }
}

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

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

相关文章

vue实现下载暂停

vue实现下载暂停

Vue实现下载暂停功能 在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法: 使用XMLHtt…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…