注意目录和修改相同的字符编码

v-if不能和v-for在同一标签元素中使用,怎么办?

先复习下v-if和v-for不能放在同一标签使用的原因:

v-for 的优先级比 v-if 的高,所以每次渲染时都会先循环再进条件判断,而又因为 v-if 会根据条件为 true 或 false来决定渲染与否的,所以如果将 v-if 和 v-for一起使用时会特别消耗性能,如果有语法检查,则会报语法的错误。

如何解决这个问题呢?

1、用template放在最外层来解决这个问题。使用template是因为它不会产生新的DOM元素,降低性能的消耗。

1 <template v-for="(item, index) in payApplyFlieList">
2     <el-descriptions-item  :label="item.templateName"  :key="index" v-if="item.updateFileId">
3         <div>
4               <span>{{ item.updateFileNane }}</span>
5         </div>
6     </el-descriptions-item>
7 </template>        

2. 如果条件出现在循环内部,不得不放在一起,可通过计算属性computed 提前过滤掉那些不需要显示的项

 1 <template>
 2   <div>
 3       <div v-for="(user,index) in activeUsers" :key="user.index" >{{ user.name }}</div>
 4   </div>
 5 </template>
 6 <script>
 7 export default {
 8   name:'A',
 9   data () {
10     return {
11       users: [{name: 'aaa',isShow: true}, {name: 'bbb',isShow: false}]
12     };
13   },
14   computed: {//通过计算属性过滤掉列表中不需要显示的项目
15     activeUsers: function () {
16       return this.users.filter(function (user) {
17         return user.isShow;//返回isShow=true的项,添加到activeUsers数组
18       })
19     }
20   }
21 };
22 </script>

 

posted @ 2022-06-29 16:33  黑使  阅读(326)  评论(0编辑  收藏  举报