当前位置:首页 > VUE

vue实现列表翻页

2026-01-19 03:48:47VUE

Vue 列表翻页实现方法

基础分页实现

在 Vue 中实现列表翻页通常需要结合分页组件和数据请求。使用 Element UI 的分页组件可以快速实现:

vue实现列表翻页

<template>
  <div>
    <ul>
      <li v-for="item in currentPageData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      listData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    currentPageData() {
      return this.listData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      )
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
    },
    fetchData() {
      // API请求获取数据
      // 设置this.listData和this.total
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

服务器端分页

对于大数据量场景,建议使用服务器端分页:

vue实现列表翻页

methods: {
  async fetchData() {
    const params = {
      page: this.currentPage,
      size: this.pageSize
    }
    const res = await api.getList(params)
    this.listData = res.data.list
    this.total = res.data.total
  },
  handleCurrentChange(val) {
    this.currentPage = val
    this.fetchData()
  }
}

无限滚动实现

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

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

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

      this.busy = true
      this.loading = true

      const res = await api.getList({ page: this.page })
      this.listData = [...this.listData, ...res.data.list]
      this.hasMore = res.data.hasMore
      this.page++

      this.busy = false
      this.loading = false
    }
  }
}
</script>

性能优化技巧

对于大型列表,使用虚拟滚动可以提高性能:

<template>
  <RecycleScroller
    class="scroller"
    :items="listData"
    :item-size="50"
    key-field="id"
    v-slot="{ item }"
  >
    <div class="item">
      {{ item.name }}
    </div>
  </RecycleScroller>
  <button @click="loadMore">加载更多</button>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'

export default {
  components: { RecycleScroller },
  data() {
    return {
      listData: []
    }
  },
  methods: {
    loadMore() {
      // 加载更多数据
    }
  }
}
</script>

实现列表翻页时需要考虑数据量大小、用户体验和性能要求,选择合适的分页方式。服务器端分页适合大数据量,客户端分页适合小数据量,无限滚动适合移动端,虚拟滚动适合超长列表。

标签: 翻页列表
分享给朋友:

相关文章

vue公共列表的实现

vue公共列表的实现

Vue 公共列表的实现方法 在 Vue 中实现公共列表组件,可以通过封装可复用的逻辑和模板来完成。以下是几种常见的实现方式: 使用 props 和 slots 通过 props 接收列表数据,使用…

vue实现列表循环

vue实现列表循环

列表循环的实现方式 在Vue中实现列表循环主要依赖于v-for指令,该指令基于源数据多次渲染元素或模板块。 基础数组循环 使用v-for遍历数组时,语法为item in items或(item, i…

如何实现翻页式h5

如何实现翻页式h5

翻页式H5的实现方法 翻页式H5通常指通过滑动或点击切换页面的交互形式,常用于营销活动、产品展示等场景。以下是几种常见的实现方式: 使用HTML5和CSS3实现基础翻页 通过CSS3的transfo…

vue滚动实现翻页

vue滚动实现翻页

实现滚动翻页的基本思路 在Vue中实现滚动翻页,通常需要监听滚动事件,判断是否滚动到页面底部,然后触发加载更多数据的操作。这可以通过结合原生DOM事件或第三方库来实现。 使用原生滚动事件监听 在Vu…

vue 实现翻页表

vue 实现翻页表

Vue 实现翻页表格的方法 在 Vue 中实现翻页表格通常需要结合表格组件和分页组件,以下是一种常见的实现方式: 安装必要的依赖 如果使用 Element UI 或 Ant Design Vue 等…

vue列表展开实现

vue列表展开实现

Vue 列表展开实现方法 使用 v-for 和 v-show 控制展开状态 通过 v-for 渲染列表项,配合 v-show 控制子内容的显示隐藏。需要为每个列表项维护一个展开状态。 <…