当前位置:首页 > VUE

vue实现翻页

2026-01-08 00:36: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>

自定义分页组件

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

vue实现翻页

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span v-for="page in pageRange" :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,
    currentPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pageRange() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  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>

结合API请求

实现分页时通常需要与后端API交互:

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_CURRENT_PAGE(state, page) {
    state.currentPage = page
  },
  SET_PAGE_SIZE(state, size) {
    state.pageSize = size
  },
  SET_TOTAL(state, total) {
    state.total = total
  }
}

export default {
  namespaced: true,
  state,
  mutations
}

这些方法可以根据具体需求进行组合和调整,实现适合项目的翻页功能。

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

相关文章

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterv…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <l…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…