当前位置: 首页 > news >正文

Vue vue/cli3 与 vue/cli4 v-for 和 v-if 一起使用冲突

问题描述

异常信息:[vue/no-use-v-if-with-v-for]
The 'this.$router.options.routers' expression inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.eslint-plugin-vue 
,如下图:

原因分析:
        error The ‘this.$router.options.routes’ expression inside ‘v-for’ directive should be replaced with a computed property that returns filtered array instead. You should not mix ‘v-for’ with ‘v-if’ vue/no-use-v-if-with-v-for

原因:v-for的优先级会高于v-if,因此v-if会重复运行在每个v-for中。

解决方法

将v-for用template标签进行包裹即可,因在该标签无特殊含义,代码如下:

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

上面代码为vue/cli4中写法,貌似vue/cli3中可以直接像下面这样写:

<el-menu router>

                    <el-submenu index="1"

                    v-for="(item,index) in this.$router.options.routers"

                    :key="index"

                    v-if="!item.hidden">

                        <template slot="title">

                            <i class="el-icon-location"></i>

                            <span>{{item.name}}</span>

                        </template>

                            <el-menu-item

                            :index="children.path"

                            v-for="(children,index) in item.children"

                            :key="index">{{children.name}}</el-menu-item>

</el-submenu>


http://www.mrgr.cn/news/14400.html

相关文章:

  • IPv4地址和子网掩码
  • 2024最新FL Studio24.1.1.4285破解版中文安装包百度云网盘下载地址
  • python脚本自动备份华为交换机配置,Console重置密码,升级系统文件
  • 全局点云配准的新思考:没有良好初值时如何配准?
  • 少儿编程入门,Scratch、Python与C++,谁能成为孩子的首选语言?
  • AI绘画SD教程 | StableDiffusion像素风格绘制,轻松制作复古怀旧像素游戏画面风格,保姆级教程带你快速入门!
  • 153页PPT丨麦肯锡-咨询公司流程管理体系规划建设方法论
  • 基于SpringBoot的银行OA系统设计与实现
  • MC新手教程
  • 【SpringCloud Alibaba】(九)学习 Gateway 服务网关
  • 微服务入门
  • 【学习笔记】技术分析-华为智驾控制器MDC Pro 610分析
  • ansible的脚本
  • 汽车DV与PV测试
  • 游戏设计师:创造虚拟世界的艺术家
  • ‍ 猫头虎 分享:Python库 PyTorch 的简介、安装、用法详解入门教程
  • 工厂模式说明
  • Prometheus 和 Grafana 通过nginx-exporter监控nginx
  • SQLite 转换字符串为日期
  • 50-java 线程池的核心主件和核心类