当前位置:首页 > VUE

vue 实现翻页

2026-01-13 00:31:18VUE

Vue 实现翻页功能

翻页功能通常用于数据分页展示,可以通过 Vue 结合分页逻辑实现。以下是常见的实现方法:

1. 基础数据分页

定义一个数据数组和分页参数,通过计算属性实现分页逻辑。

data() {
  return {
    items: [], // 所有数据
    currentPage: 1, // 当前页码
    itemsPerPage: 10 // 每页显示数量
  }
},
computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    const end = start + this.itemsPerPage
    return this.items.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.items.length / this.itemsPerPage)
  }
}

2. 分页按钮组件

创建一个分页按钮组件,处理页码切换。

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

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

    <button 
      @click="changePage(currentPage + 1)"
      :disabled="currentPage === totalPages"
    >
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: Number,
    totalPages: Number
  },
  computed: {
    pages() {
      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>

3. 与后端API结合

当数据量较大时,通常需要后端分页:

methods: {
  async fetchData(page = 1) {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page,
          per_page: this.itemsPerPage
        }
      })
      this.items = response.data.items
      this.totalItems = response.data.total
    } catch (error) {
      console.error(error)
    }
  }
},
created() {
  this.fetchData()
}

4. 使用第三方库

可以使用现成的分页组件如 vuejs-paginate

安装:

npm install vuejs-paginate

使用:

import Paginate from 'vuejs-paginate'

export default {
  components: {
    Paginate
  },
  methods: {
    changePage(page) {
      this.currentPage = page
      // 获取新页数据
    }
  }
}
<paginate
  :page-count="totalPages"
  :click-handler="changePage"
  :prev-text="'上一页'"
  :next-text="'下一页'"
  :container-class="'pagination'"
>
</paginate>

5. 样式优化

为分页组件添加基础样式:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

.pagination button.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

以上方法可以根据项目需求选择使用,前端分页适合数据量小的场景,后端分页适合大数据量情况。第三方组件可以快速实现功能,自定义组件则更灵活。

vue 实现翻页

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

相关文章

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue实现多选联动

vue实现多选联动

vue实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…