当前位置:首页 > VUE

vue中实现翻页功能

2026-02-23 11:51:05VUE

vue中实现翻页功能

在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑,以下是常见的实现方法:

使用Element UI的分页组件

安装Element UI后,可以直接使用其分页组件:

<template>
  <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>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dataList: []
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      // 模拟API请求
      const params = {
        page: this.currentPage,
        size: this.pageSize
      }
      // 实际项目中替换为真实API调用
      mockApi(params).then(res => {
        this.dataList = res.data
        this.total = res.total
      })
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

自定义分页组件

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

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

    <span 
      v-for="page in pageRange" 
      :key="page"
      @click="changePage(page)"
      :class="{active: page === currentPage}">
      {{ page }}
    </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)
    },
    pageRange() {
      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 {
  display: flex;
  gap: 5px;
}
.pagination span {
  cursor: pointer;
  padding: 5px 10px;
}
.pagination span.active {
  background: #409eff;
  color: white;
}
</style>

结合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
  }
}

export default {
  namespaced: true,
  state,
  mutations
}

组件中使用:

<template>
  <div>
    <el-pagination
      @size-change="setPageSize"
      @current-change="setCurrentPage"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total">
    </el-pagination>
  </div>
</template>

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

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

无限滚动分页

对于移动端或需要流畅滚动体验的场景,可以实现无限滚动:

<template>
  <div class="infinite-list" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy">
    <div v-for="item in items" :key="item.id">
      {{ item.content }}
    </div>
    <div v-if="loading" class="loading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      busy: false,
      loading: false,
      hasMore: true
    }
  },
  methods: {
    loadMore() {
      if (!this.hasMore || this.busy) return

      this.busy = true
      this.loading = true

      fetchData(this.page).then(res => {
        this.items = [...this.items, ...res.data]
        this.page++
        this.hasMore = res.hasMore
        this.busy = false
        this.loading = false
      })
    }
  },
  created() {
    this.loadMore()
  }
}
</script>

以上方法涵盖了Vue中实现分页的常见场景,可以根据项目需求选择合适的方式。

vue中实现翻页功能

标签: 翻页功能
分享给朋友:

相关文章

vue实现滚动翻页

vue实现滚动翻页

vue实现滚动翻页的方法 监听滚动事件 在Vue组件中,通过@scroll或window.addEventListener监听滚动事件,判断是否滚动到底部。使用clientHeight、scrollT…

vue实现桌面功能

vue实现桌面功能

Vue 实现桌面功能的方法 Vue.js 可以通过结合 Electron 或 NW.js 等框架实现桌面应用开发。以下是几种常见的方法: 使用 Vue 与 Electron 结合 Electron…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

vue 实现 功能

vue 实现 功能

Vue.js 是一个流行的前端框架,用于构建用户界面和单页应用程序。以下是一些常见的 Vue 功能实现方法: 数据绑定 Vue 的核心特性之一是数据绑定,可以通过 v-model 指令实现双向数据绑…

vue实现论坛功能

vue实现论坛功能

实现论坛功能的核心模块 论坛功能通常包含用户发帖、回帖、分类浏览、搜索等核心模块。Vue.js 作为前端框架,需配合后端 API 实现数据交互。 基础项目结构搭建 使用 Vue CLI 或 Vite…

vue怎么实现功能

vue怎么实现功能

Vue 是一个渐进式 JavaScript 框架,常用于构建用户界面。以下是一些常见的功能实现方法: 数据绑定 Vue 通过 v-model 实现双向数据绑定,适用于表单输入等场景: <te…