v-for
咱們用 v-for
指令根據一組數組的選項列表進行渲染。 v-for
指令須要以item in items
形式的特殊語法, items
是源數據數組而且 item
是數組元素迭代的別名。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' } ] } })
結果:vue
在 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' } ] } })
結果:ide
你也能夠用 of
替代 in
做爲分隔符,由於它是最接近 JavaScript 迭代器的語法:性能
<div v-for="item of items"></div>
如同 v-if
模板,你也能夠用帶有 v-for
的 <template>
標籤來渲染多個元素塊。例如:ui
<ul> <template v-for="item in items"> <li>{{ item.msg }}</li> <li class="divider"></li> </template> </ul>
你也能夠用 v-for
經過一個對象的屬性來迭代。this
<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 } } })
結果:spa
你也能夠提供第二個的參數爲鍵名:prototype
<div v-for="(value, key) in object"> {{ key }} : {{ value }} </div>
第三個參數爲索引:code
<div v-for="(value, key, index) in object"> {{ index }}. {{ key }} : {{ value }} </div>
在遍歷對象時,是按 Object.keys() 的結果遍歷,可是不能保證它的結果在不一樣的 JavaScript 引擎下是一致的。
v-for
也能夠取整數。在這種狀況下,它將重複屢次模板。
<div> <span v-for="n in 10">{{ n }}</span> </div>
結果:
瞭解組件相關知識,查看 組件 。Feel free to skip it and come back later.
在自定義組件裏,你能夠像任何普通元素同樣用 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 = '' } } })
當 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 還具備其餘用途,咱們將在後面的指南中看到其餘用途。
Vue 包含一組觀察數組的變異方法,因此它們也將會觸發視圖更新。這些方法以下:
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
你打開控制檯,而後用前面例子的 items
數組調用突變方法:example1.items.push({ message: 'Baz' })
。
變異方法(mutation method),顧名思義,會改變被這些方法調用的原始數組。相比之下,也有非變異(non-mutating method)方法,例如: filter()
, concat()
, slice()
。這些不會改變原始數組,但老是返回一個新數組。當使用非變異方法時,能夠用新數組替換舊數組:
example1.items = example1.items.filter(function (item) { return item.message.match(/Foo/) })
你可能認爲這將致使 Vue 丟棄現有 DOM 並從新渲染整個列表。幸運的是,事實並不是如此。 Vue 實現了一些智能啓發式方法來最大化 DOM 元素重用,因此用一個含有相同元素的數組去替換原來的數組是很是高效的操做。
因爲 JavaScript 的限制, Vue 不能檢測如下變更的數組:
vm.items[indexOfItem] = newValue
vm.items.length = newLength
爲了不第一種狀況,如下兩種方式將達到像 vm.items[indexOfItem] = newValue
的效果, 同時也將觸發狀態更新:
// Vue.set Vue.set(example1.items, indexOfItem, newValue)
// Array.prototype.splice` example1.items.splice(indexOfItem, 1, newValue)
避免第二種狀況,使用 splice
:
example1.items.splice(newLength)
有時,咱們想要顯示一個數組的過濾或排序副本,而不實際改變或重置原始數據。在這種狀況下,能夠建立返回過濾或排序數組的計算屬性。
例如:
<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 }) } }
|