1:用 v-for
把一个数组对应为一组元素
<ul id="example-1"> <li v-for="item in items"> {{ item.message }} </li> </ul>
在 v-for
块中,咱们能够访问全部父做用域的属性。v-for
还支持一个可选的第二个参数,即当前项的索引。数组
<ul id="example-2"> <li v-for="(item, index) in items"> {{ parentMessage }} - {{ index }} - {{ item.message }} </li> </ul>
你也能够用 of
替代 in
做为分隔符,由于它更接近 JavaScript 迭代器的语法:
浏览器
<div v-for="item of items"></div>
2:在 v-for
里使用对象,注:在遍历对象时,会按 Object.keys()
的结果遍历,可是不能保证它的结果在不一样的 JavaScript 引擎下都一致。
遍历一个对象的属性ide
<ul id="v-for-object" class="demo"> <li v-for="value in object"> {{ value }} </li> </ul>
new Vue({ el: '#v-for-object',
data: { object: {
title: 'How to do lists in Vue',
author: 'Jane Doe',
publishedAt: '2016-04-10' }
}
})
你也能够提供第二个的参数为 property 名称 (也就是键名):性能
<div v-for="(value, name) in object"> {{ name }}: {{ value }} </div>
还能够用第三个参数做为索引:this
<div v-for="(value, name, index) in object"> {{ index }}. {{ name }}: {{ value }} </div>
3:维护状态
当 Vue 正在更新使用 v-for
渲染的元素列表时,它默认使用“就地更新”的策略。spa
若是数据项的顺序被改变,Vue 将不会移动 DOM 元素来匹配数据项的顺序,而是就地更新每一个元素,而且确保它们在每一个索引位置正确渲染。prototype
这个相似 Vue 1.x 的 track-by="$index"
。code
这个默认的模式是高效的,可是只适用于不依赖子组件状态或临时 DOM 状态 (例如:表单输入值) 的列表渲染输出。component
须要使用keyorm
<div v-for="item in items" v-bind:key="item.id"> <!-- 内容 --> </div>
建议尽量在使用 v-for
时提供 key
attribute,除非遍历输出的 DOM 内容很是简单,或者是刻意依赖默认行为以获取性能上的提高。
由于它是 Vue 识别节点的一个通用机制,key
并不只与 v-for
特别关联。后面咱们将在指南中看到,它还具备其它用途。
不要使用对象或数组之类的非基本类型值做为 v-for
的 key
。请用字符串或数值类型的值。
4:数组更新检测
a:变异方法会触发视图更新:push()、
pop()、
shift()、
unshift()、
splice()、
sort()、
reverse()
b:替换数组,filter,concat,slice,新数组替换旧数组,你可能认为这将致使 Vue 丢弃现有 DOM 并从新渲染整个列表。
幸运的是,事实并不是如此。Vue 为了使得 DOM 元素获得最大范围的重用而实现了一些智能的启发式方法,因此用一个含有相同元素的数组去替换原来的数组是很是高效的操做。
c:不会变更的状况:
- 当你利用索引直接设置一个数组项时,例如:
vm.items[indexOfItem] = newValue
- 当你修改数组的长度时,例如:
vm.items.length = newLength
变通方法
// Vue.set Vue.set(vm.items, indexOfItem, newValue)
// Array.prototype.splice vm.items.splice(indexOfItem, 1, newValue)
vm.$set(vm.items, indexOfItem, newValue)
vm.items.splice(newLength)
5:对象变动检测注意事项
Vue 不能检测对象属性的添加或删除:vm,b=xxx,不识别的
能够用:Vue.set(object, propertyName, value)
方法向嵌套对象添加响应式属性
你还可使用 vm.$set
实例方法,它只是全局 Vue.set
的别名
vm.$set(vm.userProfile, 'age', 27)
有时你可能须要为已有对象赋值多个新属性
var vm = new Vue({ data: { a: 1 } }) // `vm.a` 如今是响应式的 vm.b = 2 // `vm.b` 不是响应式的
显示过滤/排序后的结果-用计算属性
有时,咱们想要显示一个数组通过过滤或排序后的版本,而不实际改变或重置原始数据。在这种状况下,能够建立一个计算属性,来返回过滤或排序后的数组
在计算属性不适用状况下:v-for 用方法
<li v-for="n in even(numbers)">{{ n }}</li>
在 v-for
里使用值范围:能够对 整数
<div> <span v-for="n in 10">{{ n }} </span> </div>
在 <template>
上使用 v-for:与v-if相似
<ul> <template v-for="item in items"> <li>{{ item.msg }}</li> <li class="divider" role="presentation"></li> </template> </ul>
v-for
与 v-if
一同使用(避免):v-for
的优先级比 v-if
更高,这意味着 v-if
将分别重复运行于每一个 v-for
循环中
在组件上使用 v-for
在自定义组件上,你能够像在任何普通元素上同样使用 v-for
。
<my-component v-for="item in items" :key="item.id"></my-component>
2.2.0+ 的版本里,当在组件上使用 v-for
时,key
如今是必须的。
<my-component v-for="(item, index) in items" v-bind:item="item" v-bind:index="index" v-bind:key="item.id" ></my-component>
为了把迭代数据传递到组件里,咱们要使用 prop
<div id="todo-list-example"> <form v-on:submit.prevent="addNewTodo"> <label for="new-todo">Add a todo</label> <input v-model="newTodoText" id="new-todo" placeholder="E.g. Feed the cat" > <button>Add</button> </form> <ul> <li is="todo-item" v-for="(todo, index) in todos" v-bind:key="todo.id" v-bind:title="todo.title" v-on:remove="todos.splice(index, 1)" ></li> </ul> </div>
Vue.component('todo-item', { template: '\ <li>\ {{ title }}\ <button v-on:click="$emit(\'remove\')">Remove</button>\ </li>\ ', props: ['title'] }) new Vue({ el: '#todo-list-example', data: { newTodoText: '', todos: [ { id: 1, title: 'Do the dishes', }, { id: 2, title: 'Take out the trash', }, { id: 3, title: 'Mow the lawn' } ], nextTodoId: 4 }, methods: { addNewTodo: function () { this.todos.push({ id: this.nextTodoId++, title: this.newTodoText }) this.newTodoText = '' } } })
注意这里的 is="todo-item"
属性。这种作法在使用 DOM 模板时是十分必要的,由于在 <ul>
元素内只有 <li>
元素会被看做有效内容。这样作实现的效果与 <todo-item>
相同,可是能够避开一些潜在的浏览器解析错误