当前位置:首页 > VUE

vue中列表怎么实现

2026-02-25 07:50:28VUE

在 Vue 中实现列表

使用 v-for 指令

Vue 提供 v-for 指令用于渲染列表数据。语法为 v-for="(item, index) in items",其中 items 是数据源数组,item 是当前项,index 是当前索引。

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

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

绑定 key 属性

为每个列表项绑定唯一的 key 属性,通常是数据中的唯一标识符(如 id)。这有助于 Vue 高效更新 DOM。

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

遍历对象

v-for 也可以遍历对象的属性,语法为 v-for="(value, key, index) in object"

vue中列表怎么实现

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

<script>
export default {
  data() {
    return {
      user: {
        name: 'John',
        age: 30
      }
    }
  }
}
</script>

使用计算属性过滤列表

通过计算属性可以对列表数据进行过滤或排序,避免直接在模板中处理复杂逻辑。

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Apple', category: 'fruit' },
        { id: 2, name: 'Carrot', category: 'vegetable' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => item.category === 'fruit')
    }
  }
}
</script>

动态更新列表

直接修改数据源数组(如使用 pushsplice)可以触发视图自动更新。

vue中列表怎么实现

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
      <button @click="removeItem(item.id)">Remove</button>
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ]
    }
  },
  methods: {
    removeItem(id) {
      this.items = this.items.filter(item => item.id !== id)
    }
  }
}
</script>

结合 v-if 条件渲染

可以在 v-for 的同一元素上使用 v-if,但更推荐在父级元素上使用 v-if 或在计算属性中过滤数据。

<template>
  <ul v-if="items.length > 0">
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
  <p v-else>No items found.</p>
</template>

使用 <template> 标签分组

如果需要渲染多个元素但不引入额外 DOM 节点,可以使用 <template> 标签。

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

性能优化

对于大型列表,可以使用虚拟滚动库(如 vue-virtual-scroller)提升性能,避免渲染所有 DOM 节点。

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

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

export default {
  components: { RecycleScroller },
  data() {
    return {
      items: [
        /* large array */
      ]
    }
  }
}
</script>

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

相关文章

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的posi…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…