vue2.0之 v-for

用 v-for 把一個數(shù)組對應(yīng)為一組元素

<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' }
    ]
  }
})

在 v-for 塊中,我們擁有對父作用域?qū)傩缘耐耆L問權(quán)限。v-for 還支持一個可選的第二個參數(shù)為當前項的索引。

<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' }
    ]
  }
})

也可以用 of 替代 in 作為分隔符,因為它是最接近 JavaScript 迭代器的語法

<div v-for="item of items"></div>

一個對象的 v-for

可以用 v-for 通過一個對象的屬性來迭代

<ul id="v-for-object" class="demo">
  <li v-for="value in object">
    {{ value }}
  </li>
</ul>

new Vue({
  el: '#v-for-object',
  data: {
    object: {
      firstName: 'John',
      lastName: 'Doe',
      age: 30
    }
  }
})

也可以提供第二個的參數(shù)為鍵名

<div v-for="(value, key) in object">
  {{ key }}: {{ value }}
</div>

第三個參數(shù)為索引

<div v-for="(value, key, index) in object">
  {{ index }}. {{ key }}: {{ value }}
</div>

key

建議盡可能在使用 v-for 時提供 key,除非遍歷輸出的 DOM 內(nèi)容非常簡單,或者是刻意依賴默認行為以獲取性能上的提升。

數(shù)組更新檢測

變異方法

  • push() 后面添加
  • pop() 后面刪除
  • shift() 前面刪除
  • unshift() 前面添加
  • splice() 向數(shù)組添加刪除元素
  • sort() 順序排序
  • reverse() 倒序排序

對象更改檢測注意事項

例如:filter(), concat() 和 slice()
總是返回一個新數(shù)組

Vue 不能檢測對象屬性的添加或刪除:

var vm = new Vue({
  data: {
    a: 1
  }
})
// `vm.a` 現(xiàn)在是響應(yīng)式的

vm.b = 2
// `vm.b` 不是響應(yīng)式的

對于已經(jīng)創(chuàng)建的實例,Vue 不能動態(tài)添加根級別的響應(yīng)式屬性。但是,可以使用 Vue.set(object, key, value) 方法向嵌套對象添加響應(yīng)式屬性

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'
})

顯示過濾/排序結(jié)果

想要顯示一個數(shù)組的過濾或排序副本,而不實際改變或重置原始數(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
    })
  }
}

在計算屬性不適用的情況下 (例如,在嵌套 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
    })
  }
}

一段取值范圍的 v-for

v-for 也可以取整數(shù)

<div>
  <span v-for="n in 10">{{ n }} </span>
</div>

v-for on a <template>

<ul>
  <template v-for="item in items">
    <li>{{ item.msg }}</li>
    <li class="divider"></li>
  </template>
</ul>

v-for with v-if

當它們處于同一節(jié)點,v-for 的優(yōu)先級比 v-if 更高,這意味著 v-if 將分別重復(fù)運行于每個 v-for 循環(huán)中

<li v-for="todo in todos" v-if="!todo.isComplete">
  {{ todo }}
</li>

如果你的目的是有條件地跳過循環(huán)的執(zhí)行,那么可以將 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

2.2.0+ 的版本里,當在組件中使用 v-for 時,key 現(xiàn)在是必須的。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,168評論 0 29
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 古今多少事 都付笑談中、青山依舊在 幾度夕陽紅!知我者終究還沒有出現(xiàn),耐得住寂寞……送自己一個字,安
    肖克濤閱讀 220評論 1 1
  • 呵,人生嘛,無非學(xué)校家庭社會,社會總會優(yōu)雅又和藹的教會你做假帳,逼迫你丟掉自尊和美好竊望,逼迫你學(xué)會習慣接受舍棄你...
    詞難達意閱讀 232評論 0 0
  • 文章作者:Tyan博客:noahsnail.com | CSDN | 簡書 本文主要是使用tensorfl...
    SnailTyan閱讀 997評論 0 1

友情鏈接更多精彩內(nèi)容