【vue+element-ui】v-for与v-if不能同时使用的解决方案之一

在Vue CLI搭建的项目中遇到v-for与v-if指令一起使用导致的错误。问题在于v-for具有较高的优先级,可能导致性能下降。解决方法是通过计算属性过滤数据,将v-if逻辑移至计算属性内部。示例代码展示了如何创建计算属性`aitemList`,用于过滤掉隐藏路由,并在v-for中使用这个计算属性。
摘要由CSDN通过智能技术生成

使用vue-cli脚手架搭建的项目,v-for与v-if不能同时使用,会报错

<el-submenu
      index="1"
       v-for="(item, index) in this.$router.options.routes"
       :key="index"
        v-if="!item.hidden"
 >
    <template slot="title"><i class="el-icon-message"></i>{{ item.name }}</template >
 </el-submenu>

解决方案之一:使用计算属性

<el-submenu index="1" v-for="(item, index) in aitemList" :key="index">
     <template slot="title"><i class="el-icon-message"></i>{{ item.name }}</template >
 </el-submenu>
computed: {
    aitemList: function () {
      return this.$router.options.routes.filter((item) => {
        return item.hidden
      })
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值