1.v-for优先级要比v-if要高
不能直接这样写,会出现警告。
<div v-for="(item,index) in checkList" :key="index" v-if="item.status">
</div>
正确的写法
<template v-for="(item,index) in checkList">
<div :key="index" v-if="item.status" ></div>
</template>
注意:不要把 v-if 和 v-for 同时用在同一个元素上,带来性能方面的浪费(每次渲染都会先循环再进行条件判断)
2.如果条件出现在循环内部,可通过计算属性computed提前过滤掉那些不需要显示的项
computed: {
items: function() {
return this.list.filter(function (item) {
return item.isShow
})
}
}
在Vue中,v-for的优先级高于v-if。将v-if与v-for写在同一元素上会导致性能浪费,应避免这种情况。正确做法是将v-for置于template标签中,然后在内部使用v-if。另外,若条件判断在循环内,可以通过计算属性computed提前过滤列表,提高效率。
1240

被折叠的 条评论
为什么被折叠?



