菜单
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP 如何 W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首页 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue 为什么、如何以及设置 Vue 第一个 SFC 页面 Vue 组件 Vue Props Vue v-for 组件 Vue $emit() Vue 穿透属性 Vue 局部样式 Vue 局部组件 Vue 插槽 Vue v-slot Vue 作用域插槽 Vue 动态组件 Vue Teleport Vue HTTP 请求 Vue 模板引用 Vue 生命周期钩子 Vue Provide/Inject Vue 路由 Vue 表单输入 Vue 动画 Vue v-for 动画 Vue 构建 Vue 组合式 API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue v-for 指令


示例

使用 v-for 指令根据数组创建哺乳动物列表

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive to create a list of mammals based on an array.</p>
  <ul>
    <li v-for="x in animals">{{ x }}</li>
  </ul>
</template>
运行示例 »

更多示例请参见下方。


定义和用法

v-for 指令用于根据数据源渲染多个元素。

v-for 指令使用 "(item, key, index) in dataSource" 语法,其中

  • "item" 别名表示 "dataSource" 中的一个元素。
  • 如果数据源是一个对象,可以使用 "key" 别名来获取属性名。
  • 如果数据源是数组或对象,可以使用 "index" 别名。
  • "dataSource" 必须是您正在循环的实际数据源的名称。

您可以自己选择 "item""key""index" 别名的名称,但顺序是 "item, key, index"

以下是 v-for 指令可以使用的数据源

数据源类型 详情
数组 v-for 循环遍历数组,可以取出并使用每个元素的元素和索引。 运行示例 »
对象 v-for 循环遍历对象。可以取出并使用属性名、值和索引。 运行示例 »
数字 v-for 渲染一个列表,其中每个项都是从一开始的数字,最后一个数字是提供的数字。也可以取出每个元素的索引。 运行示例 »
string v-for 循环遍历字符串。可以取出并使用每个字符及其索引。 运行示例 »
可迭代对象 v-for 也可以循环遍历可迭代对象。可迭代对象是使用可迭代协议的值,例如 Map 和 Set。 运行示例 »

注意: 为了优化性能,当数据源被操作时,Vue 会重用使用 v-for 创建的元素。这可能会导致奇怪的结果(此处解释)。为了防止 Vue 在使用 v-for 时错误地重用元素,您应该始终使用特殊的 key 属性和 v-bind 来唯一标识每个元素(参见示例 6)。


更多示例

示例 1

使用 v-for 指令根据数组渲染哺乳动物列表,并获取数组中每个元素的索引

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive to create a list of mammals, and the index of each mammal, based on an array.</p>
  <ul>
    <li v-for="(x, index) in animals">On index {{ index }}: "{{ x }}"</li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      animals: ['Tiger','Zebra','Wolf','Crocodile','Seal']
    };
  }
};
</script> 
运行示例 »

示例 2

使用 v-for 指令渲染属性列表,取出对象中每个属性的属性名和值

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive on an Object to create a list of the object properties and the respective property values.</p>
  <ul>
    <li v-for="(x, key) in animal">(Property name: value) = ({{ key }}: {{ x }})</li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      animal: {
        name: 'Lion',
        heightCM: 110,
        weightKG: 150
      }
    };
  }
};
</script>
运行示例 »

示例 3

使用 v-for 指令根据数字渲染列表

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive with number to render a list with that number of elements.</p>
  <ul>
    <li v-for="(x, index) in 10">Item: {{ x }}, index: {{ index }}</li>
  </ul>
</template>
运行示例 »

示例 4

使用 v-for 指令循环遍历字符串中的字符

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive to loop through the characters in a string.</p>
  <ul>
    <li v-for="(x, index) in 'Ice cream'">Item: "{{ x }}", index: {{ index }}</li>
  </ul>
</template>
运行示例 »

示例 5

使用 v-for 指令循环遍历使用可迭代协议创建的对象

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive  to render a list, based on an object created with the Iterable Protocol.</p>
  <ul>
    <li v-for="value in iterableObject">{{ value }}</li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      iterableObject: this.createIterable(['City', 'Park', 'River'])
    };
  },
  methods: {
    createIterable(array) {
      let currentIndex = -1;
      return {
        [Symbol.iterator]: function () {
          return {
            next: () => {
              if (currentIndex < array.length - 1) {
                currentIndex++;
                return { value: array[currentIndex], done: false };
              } else {
                return { done: true };
              }
            }
          };
        }
      };
    }
  }
};
</script>
运行示例 »

示例 6

使用 v-for 指令为字符串中的每个字符渲染一个 div 元素。始终建议将 v-bind:keyv-for 指令一起使用

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive with 'v-bind:key' to render DIV elements, based on a string of characters.</p>
  <div id="wrapper">
    <div v-for="x in text" v-bind:key="x">{{ x }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: 'I love ice cream.'
    };
  }
};
</script>

<style>
#wrapper {
  display: flex;
  flex-wrap: wrap;
  width: 280px;
}
#wrapper > div {
  margin: 5px;
  padding: 5px 10px;
  border: solid black 1px;
  background-color: lightgreen;
}
</style>
运行示例 »

相关页面

JavaScript 教程: JS 可迭代对象

Vue 教程: Vue v-for 指令

Vue 教程: Vue v-for 组件

Vue 教程: Vue v-for 动画

Vue 参考: Vue 'key' 属性


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持