#變異方法
作用 ?: ?改變這些方法調(diào)用的原始數(shù)組
VUE包含一組觀察數(shù)組的變異方法,他們也將觸發(fā)視圖的更新
數(shù)組方法:push()? pop()? shift()? unshift()? splace()? reverse() ?sort()
用法:在控制臺(tái)上輸入 examplat1.items.push ( { message:'Diana麗麗' } )
#非變異方法 (替換數(shù)組)
作用 : ?不改變?cè)紨?shù)組, 返回新的數(shù)組
數(shù)組方法: filter() ?concat() ?slice()
使用新數(shù)組替換就數(shù)組
example1.items = example1. items.filter ( function(item){
return item.message.match ( / foo / );
} )
VUE并沒(méi)有丟棄現(xiàn)有DOM重新渲染整個(gè)列表, 而是用一個(gè)含有相同元素的數(shù)組去替換原始數(shù)組,是一個(gè)很高效的操作
特別注意:?
由于js的限制,VUE不能檢測(cè)一下變動(dòng)的數(shù)組
1. 當(dāng)你利用數(shù)組的索引直接設(shè)置一個(gè)項(xiàng)的值時(shí): vm.items[indexofItem] = newValue
2. 當(dāng)你修改數(shù)組的長(zhǎng)度時(shí): vm.items.length=newLength
解決方法:
1. Vue,set (example1.items, indexofItem, newValue)
example1.items.splice (indexofItem, 1, newValue) ? ?//Array.prototype.splice(刪除的index, 刪除項(xiàng), 替換值)
2. example1.items.splice (newLength)
#對(duì)象更改檢測(cè)注意事項(xiàng)