目录
v-for on a <template>
列表渲染
v-for:指令根据一组数组的选项列表进行渲染。
v-for
指令需要使用item in items
形式的特殊语法,items
是源数据数组并且item
是数组元素迭代的别名。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>
var example1 = new Vue({ el: '#example-1', data: { items: [ { message: 'Foo' }, { message: 'Bar' } ] } }) var example2 = new Vue({ el: '#example-2', data: { parentMessage: 'Parent', items: [ { message: 'Foo' }, { message: 'Bar' } ] } })
v-for通过一个对象的属性来迭代
<ul id="v-for-object" class="demo"> <li v-for="value in object"> {{ value }} </li> </ul> <!-- 第二个的参数为 property 名称 (也就是键名) --> <div v-for="(value, name) in object"> {{ name }}: {{ value }} </div> <!-- 第三个参数为索引 --> <div v-for="(value, name, index) in object"> {{ index }}. {{ name }}: {{ value }} </div>
new Vue({ el: '#v-for-object', data: { object: { title: 'How to do lists in Vue', author: 'Jane Doe', publishedAt: '2016-04-10' } } })
在遍历对象时,是按
Object.keys()
的结果遍历,但是不能保证它的结果在不同的 JavaScript 引擎下是一致的。数组更新检测
变异方法:Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新。
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
example1.items.push({ message: 'Baz' })
替换数组:不会改变原始数组,但总是返回一个新数组。
filter()
,concat()
和slice()
example1.items = example1.items.filter(function (item) { return item.message.match(/Foo/) })
注意事项
由于 JavaScript 的限制,Vue 不能检测以下变动的数组:
- 当你利用索引直接设置一个项时,例如:
vm.items[indexOfItem] = newValue
- 当你修改数组的长度时,例如:
vm.items.length = newLength
var vm = new Vue({ data: { items: ['a', 'b', 'c'] } }) vm.items[1] = 'x' // 不是响应性的 vm.items.length = 2 // 不是响应性的
为了解决第一类问题,以下两种方式都可以实现和
vm.items[indexOfItem] = newValue
相同的效果,同时也将触发状态更新:// Vue.set Vue.set(vm.items, indexOfItem, newValue) // Array.prototype.splice vm.items.splice(indexOfItem, 1, newValue)
你也可以使用
vm.$set
实例方法,该方法是全局方法Vue.set
的一个别名:vm.$set(vm.items, indexOfItem, newValue)
为了解决第二类问题,你可以使用
splice
:vm.items.splice(newLength)
对象更改检测注意事项
使用
Vue.set(object, propertyName, value)
方法向嵌套对象添加响应式属性。var vm = new Vue({ data: { userProfile: { name: 'Anika' } } }) // 添加一个新的 age 属性到嵌套的 userProfile 对象 Vue.set(vm.userProfile, 'age', 27) // 使用 vm.$set 实例方法,它只是全局 Vue.set 的别名 vm.$set(vm.userProfile, 'age', 27) // 为已有对象赋予多个新属性,在这种情况下,你应该用两个对象的属性创建一个新的对象。 vm.userProfile = Object.assign({}, vm.userProfile, { age: 27, favoriteColor: 'Vue Green' })
显示过滤-排序结果
// <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 }) } } // <li v-for="n in even(numbers)">{{ n }}</li> // 在计算属性不适用的情况下 (例如,在嵌套 v-for 循环中) 你可以使用一个 method 方法 data: { numbers: [ 1, 2, 3, 4, 5 ] }, methods: { even: function (numbers) { return numbers.filter(function (number) { return number % 2 === 0 }) } }
一段取值范围的-v-for
<!-- v-for 也可以取整数。在这种情况下,它将重复多次模板。 --> <div> <span v-for="n in 10">{{ n }} </span> </div> <!-- 结果:1 2 3 4 5 6 7 8 9 10 -->
v-for on a <template>
<!-- 类似于 v-if,你也可以利用带有 v-for 的 <template> 渲染多个元素。 --> <ul> <template v-for="item in items"> <li>{{ item.msg }}</li> <li class="divider" role="presentation"></li> </template> </ul>
v-for with v-if 不推荐同时使用
<!-- 当它们处于同一节点,v-for 的优先级比 v-if 更高,这意味着 v-if 将分别重复运行于每个 v-for 循环中。当你想为仅有的一些项渲染节点时,这种优先级的机制会十分有用。如下代码只传递了未完成的 todos。 --> <li v-for="todo in todos" v-if="!todo.isComplete"> {{ todo }} </li> <!-- 如果你的目的是有条件地跳过循环的执行,那么可以将 v-if 置于外层元素 (或 <template>)上。 --> <ul v-if="todos.length"> <li v-for="todo in todos"> {{ todo }} </li> </ul> <p v-else>No todos left!</p>
组件的 v-for
<my-component v-for="item in items" :key="item.id"></my-component> <!-- 任何数据都不会被自动传递到组件里,因为组件有自己独立的作用域。为了把迭代数据传递到组件里,我们要用 props --> <my-component v-for="(item, index) in items" v-bind:item="item" v-bind:index="index" v-bind:key="item.id" ></my-component>
不自动将
item
注入到组件里的原因是,这会使得组件与v-for
的运作紧密耦合。明确组件数据的来源能够使组件在其他场合重复使用。<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>
is="todo-item"
属性。使用 DOM 模板时,因为在<ul>
元素内只有<li>
元素会被看作有效内容,使用 is 可以避开一些潜在的浏览器解析错误。Vue.component('todo-item', { template: '\ <li>\ {{ title }}\ <button v-on:click="$emit(\'remove\')">Remove</button>\ </li>\ ', props: ['title'] }) // $emit 触发当前实例上的事件。附加参数都会传给监听器回调。 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 = '' } } })