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>

相关推荐
张拭心4 分钟前
Android 17 新特性:后台音频交互限制加强
android·前端
张拭心13 分钟前
Android 17 新特性:ProfilingManager 新触发器
android·前端
黄敬峰21 分钟前
从 XMLHttpRequest 到 JSON 模拟:打通前后端通信的任督二脉
javascript
weixin_4713830321 分钟前
Taro-03-页面生命周期
前端·javascript·taro
张拭心25 分钟前
Android 17 新特性:MessageQueue 无锁实现
android·前端
Asize28 分钟前
数组数据结构底层:从灵活到陷阱
前端·javascript·算法
十九画生28 分钟前
Ajax 入门:用 XHR 理解前后端异步请求
前端·javascript·后端
yingyima29 分钟前
Python re 模块速查:从实战对比中掌握正则表达式
前端
放下华子我只抽RuiKe51 小时前
FastAPI 全栈后端(三):数据库与 ORM
前端·数据库·react.js·oracle·性能优化·前端框架·fastapi
源图客2 小时前
境外电商 - 龙虾智能体-综合选品推荐报告
开发语言·javascript·ecmascript