ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 列表渲染 [](https://vuefe.cn/guide/list.html#v-for) ## [](https://vuefe.cn/guide/list.html#v-for)[](https://vuefe.cn/guide/list.html#v-for "v-for")`v-for` 我们用 `v-for` 指令根据一组数组的选项列表进行渲染。 `v-for` 指令需要以 `item in items` 形式的特殊语法, `items`是源数据数组并且 `item` 是数组元素迭代的别名。 [](https://vuefe.cn/guide/list.html#基本用法) ### [](https://vuefe.cn/guide/list.html#基本用法)[](https://vuefe.cn/guide/list.html#基本用法 "基本用法")基本用法 ~~~ <ul id="example-1"> <li v-for="item in items"> {{ item.message }} </li></ul> ~~~ ~~~ var example1 = new Vue({ el: '#example-1', data: { items: [ {message: 'foo' }, {message: 'Bar' } ] }}) ~~~ 结果: * Foo * Bar 在 `v-for` 块中,我们拥有对父作用域属性的完全访问权限。 `v-for` 还支持一个可选的第二个参数为当前项的索引。 ~~~ <ul id="example-2"> <li v-for="(item, index) in items"> {{ parentMessage }} - {{ index }} - {{ item.message }} </li></ul> ~~~ ~~~ var example2 = new Vue({ el: '#example-2', data: { parentMessage: 'Parent', items: [ { message: 'Foo' }, { message: 'Bar' } ] }}) ~~~ 结果: * Parent - 0 - Foo * Parent - 1 - Bar 你也可以用 `of` 替代 `in` 作为分隔符,因为它是最接近 JavaScript 迭代器的语法: ~~~ <div v-for="item of items"></div> ~~~ [](https://vuefe.cn/guide/list.html#Template-v-for) ### [](https://vuefe.cn/guide/list.html#Template-v-for)[](https://vuefe.cn/guide/list.html#Template-v-for "Template v-for")Template v-for 如同 `v-if` 模板,你也可以用带有 `v-for` 的 `<template>` 标签来渲染多个元素块。例如: ~~~ <ul> <template v-for="item in items"> <li>{{ item.msg }}</li> <li class="divider"></li> </template></ul> ~~~ [](https://vuefe.cn/guide/list.html#对象迭代-v-for) ### [](https://vuefe.cn/guide/list.html#对象迭代-v-for)[](https://vuefe.cn/guide/list.html#对象迭代-v-for "对象迭代 v-for")对象迭代 v-for 你也可以用 `v-for` 通过一个对象的属性来迭代。 ~~~ <ul id="repeat-object" class="demo"> <li v-for="value in object"> {{ value }} </li></ul> ~~~ ~~~ new Vue({ el: '#repeat-object', data: { object: { FirstName: 'John', LastName: 'Doe', Age: 30 } }}) ~~~ 结果: * John * Doe * 30 你也可以提供第二个的参数为键名: ~~~ <div v-for="(value, key) in object"> {{ key }} : {{ value }}</div> ~~~ 第三个参数为索引: ~~~ <div v-for="(value, key, index) in object"> {{ index }}. {{ key }} : {{ value }}</div> ~~~ 在遍历对象时,是按 Object.keys() 的结果遍历,但是不能保证它的结果在不同的 JavaScript 引擎下是一致的。 [](https://vuefe.cn/guide/list.html#整数迭代-v-for) ### [](https://vuefe.cn/guide/list.html#整数迭代-v-for)[](https://vuefe.cn/guide/list.html#整数迭代-v-for "整数迭代 v-for")整数迭代 v-for `v-for` 也可以取整数。在这种情况下,它将重复多次模板。 ~~~ <div> <span v-for="n in 10">{{ n }}</span></div> ~~~ 结果: 1 2 3 4 5 6 7 8 9 10 [](https://vuefe.cn/guide/list.html#组件-和-v-for) ### [](https://vuefe.cn/guide/list.html#组件-和-v-for)[](https://vuefe.cn/guide/list.html#组件-和-v-for "组件 和 v-for")组件 和 v-for > 了解组件相关知识,查看 [组件](https://vuefe.cn/guide/components.html) 。 在自定义组件里,你可以像任何普通元素一样用 `v-for` 。 ~~~ <my-component v-for="item in items"></my-component> ~~~ 然而他不能自动传递数据到组件里,因为组件有自己独立的作用域。为了传递迭代数据到组件里,我们要用 `props` : ~~~ <my-component v-for="(item, index) in items" v-bind:item="item" v-bind:index="index"></my-component> ~~~ 不自动注入 `item` 到组件里的原因是,因为这使得组件会紧密耦合到 `v-for` 如何运作。在一些情况下,明确数据的来源可以使组件可重用。 下面是一个简单的 todo list 完整的例子: ~~~ <div id="todo-list-example"> <input v-model="newTodoText" v-on:keyup.enter="addNewTodo" placeholder="Add a todo" > <ul> <li is="todo-item" v-for="(todo, index) in todos" v-bind:title="todo" v-on:remove="todos.splice(index, 1)" ></li> </ul></div> ~~~ ~~~ Vue.component('todo-item', { template: '\ <li>\ {{ title }}\ <button v-on:click="$emit(\'remove\')">X</button>\ </li>\ ', props: ['title']})new Vue({ el: '#todo-list-example', data: { newTodoText: '', todos: [ 'Do the dishes', 'Take out the trash', 'Mow the lawn' ] }, methods: { addNewTodo: function () { this.todos.push(this.newTodoText) this.newTodoText = '' } }}) ~~~ * Do the dishes X * Take out the trash X * Mow the lawn X [](https://vuefe.cn/guide/list.html#key) ## [](https://vuefe.cn/guide/list.html#key)[](https://vuefe.cn/guide/list.html#key "key")key 当 Vue.js 用 `v-for` 正在更新已渲染过的元素列表时,它默认用 “就地复用” 策略。如果数据项的顺序被改变,而不是移动 DOM 元素来匹配数据项的顺序, Vue 将简单复用此处每个元素,并且确保它在特定索引下显示已被渲染过的每个元素。这个类似 Vue 1.x 的 `track-by="$index"` 。 这个默认的模式是有效的,但是只适用于不依赖子组件状态或临时 DOM 状态(例如:表单输入值)的列表渲染输出。 为了给 Vue 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 `key`属性。理想的 `key` 值是每项都有唯一 id。这个特殊的属性相当于 Vue 1.x 的 `track-by` ,但它的工作方式类似于一个属性,所以你需要用 `v-bind` 来绑定动态值(在这里使用简写): ~~~ <div v-for="item in items" :key="item.id"> <!-- 内容 --></div> ~~~ 建议尽可能使用 `v-for` 来提供 `key` ,除非迭代 DOM 内容足够简单,或者你是故意要依赖于默认行为来获得性能提升。 因为它是 Vue 识别节点的一个通用机制, `key` 并不特别与 `v-for` 关联,key 还具有其他用途,我们将在后面的指南中看到其他用途。 [](https://vuefe.cn/guide/list.html#数组更新检测) ## [](https://vuefe.cn/guide/list.html#数组更新检测)[](https://vuefe.cn/guide/list.html#数组更新检测 "数组更新检测")数组更新检测 [](https://vuefe.cn/guide/list.html#变异方法) ### [](https://vuefe.cn/guide/list.html#变异方法)[](https://vuefe.cn/guide/list.html#变异方法 "变异方法")变异方法 Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新。这些方法如下: * `push()` * `pop()` * `shift()` * `unshift()` * `splice()` * `sort()` * `reverse()` 你打开控制台,然后用前面例子的 `items` 数组调用突变方法:`example1.items.push({ message: 'Baz' })` 。 [](https://vuefe.cn/guide/list.html#重塑数组) ### [](https://vuefe.cn/guide/list.html#重塑数组)[](https://vuefe.cn/guide/list.html#重塑数组 "重塑数组")重塑数组 变异方法(mutation method),顾名思义,会改变被这些方法调用的原始数组。相比之下,也有非变异(non-mutating method)方法,例如: `filter()`, `concat()`, `slice()` 。这些不会改变原始数组,但总是返回一个新数组。当使用用非变异方法时,可以用新数组替换旧数组: ~~~ example1.items = example1.items.filter(function (item) { return item.message.match(/Foo/)}) ~~~ 你可能认为这将导致 Vue 丢弃现有 DOM 并重新渲染整个列表。幸运的是,事实并非如此。 Vue 实现了一些智能启发式方法来最大化 DOM 元素重用,所以用一个含有相同元素的数组去替换原来的数组是非常高效的操作。 [](https://vuefe.cn/guide/list.html#注意事项) ### [](https://vuefe.cn/guide/list.html#注意事项)[](https://vuefe.cn/guide/list.html#注意事项 "注意事项")注意事项 由于 JavaScript 的限制, Vue 不能检测以下变动的数组: 1. 当你直接设置一个项的索引时,例如: `vm.items[indexOfItem] = newValue` 2. 当你修改数组的长度时,例如: `vm.items.length = newLength` 为了避免第一种情况,以下两种方式将达到像 `vm.items[indexOfItem] = newValue` 的效果, 同时也将触发状态更新: ~~~ // Vue.setVue.set(example1.items, indexOfItem, newValue) ~~~ ~~~ // Array.prototype.splice`example1.items.splice(indexOfItem, 1, newValue) ~~~ 避免第二种情况,使用 `splice`: ~~~ example1.items.splice(newLength) ~~~ [](https://vuefe.cn/guide/list.html#显示过滤-排序结果) ## [](https://vuefe.cn/guide/list.html#显示过滤-排序结果)[](https://vuefe.cn/guide/list.html#显示过滤-排序结果 "显示过滤/排序结果")显示过滤/排序结果 有时,我们想要显示一个数组的过滤或排序副本,而不实际改变或重置原始数据。在这种情况下,可以创建返回过滤或排序数组的计算属性。 例如: ~~~ <li v-for="n in evenNumbers">{{ n }}</li> ~~~ ~~~ data: { numbers: [ 1, 2, 3, 4, 5 ]},computed: { evenNumbers: function () { return this.numbers.filter(function (number) { return number % 2 === 0 }) }} ~~~ 或者,您也可以使用在计算属性是不可行的 method 方法 (例如,在嵌套 `v-for` 循环中): ~~~ <li v-for="n in even(numbers)">{{ n }}</li> ~~~ ~~~ data: { numbers: [ 1, 2, 3, 4, 5 ]},methods: { even: function (numbers) { return numbers.filter(function (number) { return number % 2 === 0 }) }} ~~~