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>

相关推荐
Jinxiansen02117 分钟前
Vue 3 实战:【加强版】公司通知推送(WebSocket + token 校验 + 心跳机制)
前端·javascript·vue.js·websocket·typescript
MrSkye8 分钟前
React入门:组件化思想?数据驱动?
前端·react.js·面试
BillKu17 分钟前
Java解析前端传来的Unix时间戳
java·前端·unix
@Mr_LiuYang17 分钟前
网页版便签应用开发:HTML5本地存储与拖拽交互实践
前端·交互·html5·html5便签应用
JohnYan20 分钟前
Bun技术评估 - 05 SQL
javascript·后端·bun
JacksonGao21 分钟前
一分钟带你了解React Fiber的工作单元结构!
前端·react.js
前端农民晨曦22 分钟前
深入浏览器事件循环与任务队列架构
前端·javascript·面试
Vhen24 分钟前
Taro Echarts封装内外环形饼图
前端
Spider_Man37 分钟前
JavaScript对象那些坑:初学者必踩的“陷阱”与进阶秘籍
前端·javascript
海螺先生1 小时前
Cursor 高阶使用指南:AI 辅助开发的深度整合
前端