详解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)
信赖看了本案牍例你已经把握了办法,更多出色请关注百分百源码网其它相关文章!
相关阅读:
在HTML中怎样用<a>标签编写个人珍藏夹
HTML用img标签做图
HTML里的常见问题一
以上就是详解Vue列表渲染的具体内容,更多请关注百分百源码网其它相关文章!