v-for
我們用 v-for 指令根據(jù)一組數(shù)組的選項(xiàng)列表進(jìn)行渲染。 v-for 指令需要以 item in items 形式的特殊語(yǔ)法, items 是源數(shù)據(jù)數(shù)組并且 item 是數(shù)組元素迭代的別名。
<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' }
]
}
})
結(jié)果:
在 v-for 塊中,我們擁有對(duì)父作用域?qū)傩缘耐耆L問(wèn)權(quán)限。 v-for 還支持一個(gè)可選的第二個(gè)參數(shù)為當(dāng)前項(xiàng)的索引。
<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' }
]
}
})
結(jié)果:
你也可以用 of 替代 in 作為分隔符,因?yàn)樗亲罱咏?JavaScript 迭代器的語(yǔ)法:
<div v-for="item of items"></div>
如同 v-if 模板,你也可以用帶有 v-for 的 <template> 標(biāo)簽來(lái)渲染多個(gè)元素塊。例如:
<ul>
<template v-for="item in items">
<li>{{ item.msg }}</li>
<li class="divider"></li>
</template>
</ul>
你也可以用 v-for 通過(guò)一個(gè)對(duì)象的屬性來(lái)迭代。
<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
}
}
})
結(jié)果:
你也可以提供第二個(gè)的參數(shù)為鍵名:
<div v-for="(value, key) in object">
{{ key }} : {{ value }}
</div>
第三個(gè)參數(shù)為索引:
<div v-for="(value, key, index) in object">
{{ index }}. {{ key }} : {{ value }}
</div>
在遍歷對(duì)象時(shí),是按 Object.keys() 的結(jié)果遍歷,但是不能保證它的結(jié)果在不同的 JavaScript 引擎下是一致的。
v-for 也可以取整數(shù)。在這種情況下,它將重復(fù)多次模板。
<div>
<span v-for="n in 10">{{ n }}</span>
</div>
結(jié)果:
1 2 3 4 5 6 7 8 9 10
查看“組件”一節(jié),了解組件相關(guān)知識(shí)。您也可以先跳過(guò)本小節(jié),以后再回來(lái)查看。
在自定義組件里,你可以像任何普通元素一樣用 v-for 。
<my-component v-for="item in items"></my-component>
然而他不能自動(dòng)傳遞數(shù)據(jù)到組件里,因?yàn)榻M件有自己獨(dú)立的作用域。為了傳遞迭代數(shù)據(jù)到組件里,我們要用 props :
<my-component
v-for="(item, index) in items"
v-bind:item="item"
v-bind:index="index">
</my-component>
不自動(dòng)注入 item 到組件里的原因是,因?yàn)檫@使得組件會(huì)緊密耦合到 v-for 如何運(yùn)作。在一些情況下,明確數(shù)據(jù)的來(lái)源可以使組件可重用。
下面是一個(gè)簡(jiǎn)單的 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 = ''
}
}
})
當(dāng) Vue.js 用 v-for 正在更新已渲染過(guò)的元素列表時(shí),它默認(rèn)用 “就地復(fù)用” 策略。如果數(shù)據(jù)項(xiàng)的順序被改變,而不是移動(dòng) DOM 元素來(lái)匹配數(shù)據(jù)項(xiàng)的順序, Vue 將簡(jiǎn)單復(fù)用此處每個(gè)元素,并且確保它在特定索引下顯示已被渲染過(guò)的每個(gè)元素。這個(gè)類似 Vue 1.x 的 track-by="$index" 。
這個(gè)默認(rèn)的模式是有效的,但是只適用于不依賴子組件狀態(tài)或臨時(shí) DOM 狀態(tài)(例如:表單輸入值)的列表渲染輸出。
為了給 Vue 一個(gè)提示,以便它能跟蹤每個(gè)節(jié)點(diǎn)的身份,從而重用和重新排序現(xiàn)有元素,你需要為每項(xiàng)提供一個(gè)唯一 key 屬性。理想的 key 值是每項(xiàng)都有唯一 id。這個(gè)特殊的屬性相當(dāng)于 Vue 1.x 的 track-by ,但它的工作方式類似于一個(gè)屬性,所以你需要用 v-bind來(lái)綁定動(dòng)態(tài)值(在這里使用簡(jiǎn)寫):
<div v-for="item in items" :key="item.id">
<!-- 內(nèi)容 -->
</div>
建議盡可能使用 v-for 來(lái)提供 key ,除非迭代 DOM 內(nèi)容足夠簡(jiǎn)單,或者你是故意要依賴于默認(rèn)行為來(lái)獲得性能提升。
因?yàn)樗?Vue 識(shí)別節(jié)點(diǎn)的一個(gè)通用機(jī)制, key 并不特別與 v-for 關(guān)聯(lián),key 還具有其他用途,我們將在后面的指南中看到其他用途。
Vue 包含一組觀察數(shù)組的變異方法,所以它們也將會(huì)觸發(fā)視圖更新。這些方法如下:
你打開控制臺(tái),然后用前面例子的 items 數(shù)組調(diào)用變異方法:example1.items.push({ message: 'Baz' }) 。
變異方法(mutation method),顧名思義,會(huì)改變被這些方法調(diào)用的原始數(shù)組。相比之下,也有非變異(non-mutating method)方法,例如: filter(), concat(), slice() 。這些不會(huì)改變?cè)紨?shù)組,但總是返回一個(gè)新數(shù)組。當(dāng)使用非變異方法時(shí),可以用新數(shù)組替換舊數(shù)組:
example1.items = example1.items.filter(function (item) {
return item.message.match(/Foo/)
})
你可能認(rèn)為這將導(dǎo)致 Vue 丟棄現(xiàn)有 DOM 并重新渲染整個(gè)列表。幸運(yùn)的是,事實(shí)并非如此。 Vue 實(shí)現(xiàn)了一些智能啟發(fā)式方法來(lái)最大化 DOM 元素重用,所以用一個(gè)含有相同元素的數(shù)組去替換原來(lái)的數(shù)組是非常高效的操作。
由于 JavaScript 的限制, Vue 不能檢測(cè)以下變動(dòng)的數(shù)組:
為了避免第一種情況,以下兩種方式將達(dá)到像 vm.items[indexOfItem] = newValue 的效果, 同時(shí)也將觸發(fā)狀態(tài)更新:
// Vue.set
Vue.set(example1.items, indexOfItem, newValue)
// Array.prototype.splice`
example1.items.splice(indexOfItem, 1, newValue)
避免第二種情況,使用 splice:
example1.items.splice(newLength)
有時(shí),我們想要顯示一個(gè)數(shù)組的過(guò)濾或排序副本,而不實(shí)際改變或重置原始數(shù)據(jù)。在這種情況下,可以創(chuàng)建返回過(guò)濾或排序數(shù)組的計(jì)算屬性。
例如:
<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
})
}
}
或者,您也可以在計(jì)算屬性不適用的情況下 (例如,在嵌套 v-for 循環(huán)中) 使用 method 方法:
<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
})
}
}
更多建議: