当前位置:首页 > VUE

vue 实现列表

2026-01-07 19:12:07VUE

Vue 实现列表的方法

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

基础列表渲染

使用 v-for 指令遍历数组,渲染列表项。item 是当前项,index 是索引(可选)。

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

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

带键值的列表

为每个列表项添加唯一的 key,提升渲染性能。通常使用 id 而非 index 作为 key

vue 实现列表

<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' },
        { id: 3, name: 'Item 3' }
      ]
    };
  }
};
</script>

对象列表渲染

遍历对象属性,value 是属性值,key 是属性名(可选)。

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

<script>
export default {
  data() {
    return {
      object: {
        name: 'Vue',
        version: '3.x',
        language: 'JavaScript'
      }
    };
  }
};
</script>

使用计算属性过滤列表

通过计算属性动态过滤或处理列表数据。

vue 实现列表

<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' },
        { id: 3, name: 'Banana', category: 'fruit' }
      ]
    };
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => item.category === 'fruit');
    }
  }
};
</script>

动态列表操作

结合方法实现列表的动态增删。

<template>
  <div>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item }}
        <button @click="removeItem(index)">Remove</button>
      </li>
    </ul>
    <input v-model="newItem" @keyup.enter="addItem" placeholder="Add item">
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      newItem: ''
    };
  },
  methods: {
    addItem() {
      if (this.newItem.trim()) {
        this.items.push(this.newItem);
        this.newItem = '';
      }
    },
    removeItem(index) {
      this.items.splice(index, 1);
    }
  }
};
</script>

使用组件渲染列表

将列表项封装为可复用的组件。

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

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

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

注意事项

  • 始终为 v-for 提供唯一的 key,避免直接使用 index 作为 key(除非列表是静态的)。
  • 对于复杂列表逻辑,优先使用计算属性或方法处理数据。
  • 列表数据更新时,确保使用响应式方法(如 pushsplice),或直接替换整个数组。

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

相关文章

vue实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个…

vue实现搜索

vue实现搜索

Vue 实现搜索功能 在 Vue 中实现搜索功能通常涉及数据绑定、事件监听和过滤逻辑。以下是几种常见的实现方式: 使用计算属性过滤列表 <template> <div>…

vue 实现目录

vue 实现目录

Vue 实现目录的方法 在 Vue 项目中实现目录功能,可以通过以下几种方式实现,具体取决于需求和技术栈。 使用 Vue Router 实现路由目录 Vue Router 是 Vue.js 的官方路…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue 提供…

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…