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>

相关推荐
让开,我要吃人了3 分钟前
HarmonyOS应用开发( Beta5.0)HOS-用户认证服务:面部识别
服务器·前端·华为·移动开发·嵌入式·harmonyos·鸿蒙
小小李程序员5 分钟前
Redis地理数据类型GEO
前端·redis·bootstrap
黑客大佬12 分钟前
安全工具 | 使用Burp Suite的10个小tips
运维·前端·网络·学习·安全·云计算·腾讯云
fmc1211041 小时前
Excel文档的读取(3)
java·前端·python
生椰拿铁You1 小时前
CSS——盒子模型
前端·css
不知名的小Q1 小时前
如何动态获取路由上的参数
前端·javascript·vue.js
大鸡腿最好吃1 小时前
react&webpack老项目开发环境增加vite
前端·react.js·webpack
四季予你661 小时前
手写call、apply、bind
前端·javascript·vue.js
椰椰椰耶1 小时前
【HTML】HTML页面和常见标签
前端·html
试着奔跑的菜鸟2 小时前
Vue:通过js控制css变量 - 一键修改全局样式
前端·javascript·css·vue.js