詳細說明Vue下文渲染
發表時間:2023-09-13 來源:明輝站整理相關軟件相關文章人氣:11
[摘要]這次給大家帶來詳解Vue列表渲染,詳解Vue列表渲染的注意事項有哪些,下面就是實戰案例,一起來看一下。變異方法 (mutation method),顧名思義,會改變被這些方法調用的原始數組。相比之下,也有非變異 (non-mutating method) 方法,例如:filter(), conca...
這次給大家帶來詳解Vue列表渲染,詳解Vue列表渲染的
注意事項有哪些,下面就是實戰案例,一起來看一下。
變異方法 (mutation method),顧名思義,會改變被這些方法調用的原始數組。相比之下,也有非變異 (non-mutating method) 方法,例如:filter(), concat() 和 slice() 。這些不會改變原始數組,但總是返回一個新數組。當使用非變異方法時,可以用新數組替換舊數組:
example1.items = example1.items.filter(function (item) { return item.message.match(/Foo/)})
<div id="example">
<div>
<button @click="concat()">concat</button>
<button @click="slice()">slice</button>
<button @click="filter()">filter</button>
</div>
<ul>
<li v-for="item in items">
{{item.list}}
</li>
</ul>
</div>
<script>
var example = new Vue({
el:"#example",
data:{
items:[
{list:5},
{list:6},
{list:7}
],
addValue:{list:10}
},
methods:{
concat(){
this.items= this.items.concat(this.addValue)
},
slice(){
this.items = this.items.slice(1)
},
filter(){
this.items = this.items.filter(function(item,index,arr) {
return (index > 0)
})
}
}
})
以上操作并不會導致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)
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
在HTML中如何用<a>標簽編寫個人收藏夾
HTML用img標簽做圖
HTML里的常見問題一
以上就是詳解Vue列表渲染的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。