您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

详解Vue列表渲染

2026/1/25 15:45:02发布15次查看
这次给大家带来详解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)
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
相关阅读:
在html中如何用3499910bf9dac5ae3c52d5ede7383485标签编写个人收藏夹
html用img标签做图
html里的常见问题一
以上就是详解vue列表渲染的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product