当前位置:首页 > VUE

vue实现列表组件

2026-01-17 04:48:40VUE

Vue 列表组件实现方法

基础列表渲染

使用 v-for 指令渲染数组数据,需配合 :key 提升性能:

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ]
    }
  }
}
</script>

动态列表属性

通过计算属性实现过滤或排序:

computed: {
  filteredItems() {
    return this.items.filter(item => item.active)
  }
}

分页功能实现

结合分页逻辑与列表渲染:

vue实现列表组件

data() {
  return {
    currentPage: 1,
    itemsPerPage: 5
  }
},
computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    return this.items.slice(start, start + this.itemsPerPage)
  }
}

拖拽排序功能

使用 vuedraggable 插件实现:

<draggable v-model="items" @end="onDragEnd">
  <div v-for="item in items" :key="item.id">
    {{ item.name }}
  </div>
</draggable>

无限滚动加载

监听滚动事件加载更多数据:

vue实现列表组件

methods: {
  handleScroll() {
    const bottom = window.innerHeight + window.scrollY >= document.body.offsetHeight
    if (bottom && !this.loading) {
      this.loadMore()
    }
  }
}

性能优化技巧

对于大型列表使用虚拟滚动:

<RecycleScroller
  :items="items"
  :item-size="50"
  key-field="id"
>
  <template v-slot="{ item }">
    {{ item.name }}
  </template>
</RecycleScroller>

组件化设计

将列表项提取为独立组件:

<template>
  <ul>
    <ListItem 
      v-for="item in items"
      :key="item.id"
      :item="item"
      @delete="handleDelete"
    />
  </ul>
</template>

标签: 组件列表
分享给朋友:

相关文章

vue组件传值实现分页

vue组件传值实现分页

Vue 组件传值实现分页 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit向…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&g…

vue实现列表循环

vue实现列表循环

Vue 列表循环的实现方法 在 Vue 中,可以通过 v-for 指令实现列表循环渲染。以下是几种常见的实现方式: 基础列表渲染 <template> <ul>…

vue实现WiFi列表

vue实现WiFi列表

获取WiFi列表的Vue实现 使用Vue获取WiFi列表需要结合浏览器API或后端服务,因为浏览器出于安全限制无法直接访问系统WiFi信息。以下是两种常见实现方式: 方法一:通过Web API获取附…

vue实现tooltips组件

vue实现tooltips组件

Vue实现Tooltips组件的方法 基础实现:使用Vue指令 创建一个自定义指令v-tooltip,通过鼠标事件触发提示框的显示与隐藏。 Vue.directive('tooltip', {…