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>

相关推荐
于慨1 分钟前
java Web
java·开发语言·前端
徐小夕@趣谈前端1 分钟前
借助AI,1周,0后端成本,我们开源了一款Office预览SDK
前端·人工智能·开源·node.js·编辑器·github·格式工厂
Dxy12393102163 分钟前
HTML中图表学习:从基础到实战指南
前端·学习·html
We་ct3 分钟前
LeetCode 53. 最大子数组和:两种高效解法(动态规划+分治)
前端·算法·leetcode·typescript·动态规划·分治
我爱学习_zwj3 分钟前
设计模式-3(装饰器模式)
前端·设计模式·装饰器模式
Jinuss8 分钟前
React 19 新特性:`useOptimistic` Hook 完整指南
前端·javascript·react.js
清汤饺子9 分钟前
$20 的 Cursor Pro 额度,这样用一个月都花不完
前端·javascript·后端
a1117769 分钟前
MD 架构图生成器(html 开源)
前端·开源·html
肠胃炎13 分钟前
树形选择器组件封装
前端·flutter
CHU72903518 分钟前
一番赏爬塔闯关小程序前端功能玩法设计解析
前端·小程序