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>

相关推荐
ZC跨境爬虫3 分钟前
跟着 MDN 学 HTML day_57:(HTML 表格进阶特性与无障碍实践)
java·前端·javascript·ui·html·音视频
Moment3 分钟前
刷 Reddit 1 小时没结果?我用这个方法 10 秒挖出真实需求
前端·javascript·后端
折翅嘀皇虫5 分钟前
【无标题】steal_work_thread_pool
服务器·前端·算法
w_t_y_y6 分钟前
VUE组件配置项(零)概述
前端·javascript·vue.js
水云桐程序员8 分钟前
Web应用的分类
前端·javascript·vue.js·react.js·webkit
Jack N8 分钟前
2026 Web 网站性能优化指南
前端·性能优化
yqcoder8 分钟前
TypeScript 进阶:如何精准获取对象的所有 Key?
javascript·ubuntu·typescript
UXbot9 分钟前
支持移动端原型绘制的 AI 工具核心功能对比(2026):5 款主流平台能力横向评测
前端·低代码·ui·交互·原型模式·web app
ZC跨境爬虫25 分钟前
跟着 MDN 学 HTML day_54:(深入掌握 XSLTProcessor API)
前端·javascript·ui·html·媒体
一块小土坷垃28 分钟前
# ArchiCAD 29.0.2(畅享版):专为建筑师打造的BIM高效建模工具
前端·数据库·macos·开源软件