当前位置:首页 > VUE

vue列表实现

2026-03-07 00:26:48VUE

Vue 列表实现方法

在 Vue 中实现列表渲染通常使用 v-for 指令,结合数组或对象数据动态生成 DOM 元素。以下是几种常见的实现方式:

基础列表渲染

使用 v-for 指令遍历数组,生成列表项。item 是当前元素,index 是可选索引。

vue列表实现

<template>
  <ul>
    <li v-for="(item, index) in items" :key="index">
      {{ item.text }}
    </li>
  </ul>
</template>

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

对象遍历

v-for 也可以遍历对象的属性,参数顺序为 (value, key, index)

<template>
  <ul>
    <li v-for="(value, key) in object" :key="key">
      {{ key }}: {{ value }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      object: {
        title: 'Vue Guide',
        author: 'Vue Team',
        published: '2021'
      }
    }
  }
}
</script>

使用组件渲染列表

将列表项封装为可复用的组件,通过 props 传递数据。

vue列表实现

<template>
  <ul>
    <list-item
      v-for="(item, index) in items"
      :key="item.id"
      :item="item"
    />
  </ul>
</template>

<script>
import ListItem from './ListItem.vue'

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

条件渲染与列表结合

通过 v-if 或计算属性实现条件筛选后的列表渲染。

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1', isActive: true },
        { id: 2, text: 'Item 2', isActive: false }
      ]
    }
  },
  computed: {
    activeItems() {
      return this.items.filter(item => item.isActive)
    }
  }
}
</script>

性能优化

对于大型列表,可通过虚拟滚动(如 vue-virtual-scroller)减少 DOM 节点数量。

<template>
  <RecycleScroller
    class="scroller"
    :items="largeList"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <div class="item">
        {{ item.text }}
      </div>
    </template>
  </RecycleScroller>
</template>

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

export default {
  components: { RecycleScroller },
  data() {
    return {
      largeList: Array(1000).fill().map((_, i) => ({
        id: i,
        text: `Item ${i}`
      }))
    }
  }
}
</script>

注意事项

  1. key 属性:必须为每项提供唯一 key,通常使用 ID 而非索引,避免渲染问题。
  2. 数组更新:直接修改数组(如 this.items[0] = newValue)不会触发视图更新,应使用 Vue.set 或数组方法(push, splice 等)。
  3. 性能监控:超长列表需配合虚拟滚动或分页,避免内存问题。

标签: 列表vue
分享给朋友:

相关文章

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…