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>

相关推荐
用户059540174464 小时前
asyncio 踩坑实录:这个问题坑了我3小时,差点让线上服务崩掉
前端·css
喂哟咦4 小时前
关于用codex两周写了一个epub阅读器这件事
前端·javascript
CDwenhuohuo4 小时前
前端文件预览
开发语言·前端·javascript
test_00014 小时前
JavaScript展开运算符的三个妙用
前端
前端尤雨西4 小时前
ElementPlus 源码之 packages 目录
前端·element
我要让全世界知道我很低调4 小时前
扔掉你的 Playwright MCP,拥抱 Playwright CLI
前端
Daybreak4 小时前
从 npm 到 pnpm:包管理器演进与 Monorepo 依赖冲突求生
前端
Restart-AHTCM4 小时前
AI 时代的大前端崛起,TypeScript 重塑前端开发
前端·人工智能·typescript·ai编程·a
008爬虫实战录4 小时前
【最新猿人学】 验证码 - 图文点选 文字验证码识别
前端·javascript
一叶飘零晋4 小时前
【(一)Electron 使用之如何用vite+vue3搭建初始框架】
前端·javascript·electron