当前位置: 首页 > news >正文

Vue2 使用插件 Volar 报错:<template v-for> key should be placed on the <template> tag.

目录

问题描述

版本描述

问题定位

问题解决

VS Code 插件地址


问题描述

        在 VS Code 上使用插件 Volar 开发 Vue3 项目,然后去改 Vue2 项目时,对没有放在<template v-for> 元素上的 :key,会提示 <template v-for> key should be placed on the <template> tag.

原先 Vue2 项目开发时使用插件 Vuter。

Vue2 代码示例

<template v-for="(item, index) in list"><div :key="index" />
</template>

版本描述

Vue2 项目Vue3 项目
vue@2.6.14vue@3.2.19
eslint@4.19.1eslint@6.8.0
eslint-plugin-vue@4.7.1eslint-plugin-vue@7.18.0
babel-eslint@8.2.6babel-eslint@10.1.0

问题定位

eslint-plugin-vue 规则上关于key是否能置于<template v-for>上的冲突。

Priority A: Essential for Vue.js 2.x
规则vue/no-v-for-template-key: Disallow key attribute on <template v-for>

Priority A: Essential for Vue.js 3.x
规则vue/no-v-for-template-key-on-child: Disallow key of <template v-for> placed on child elements

上面这两个规则都是从版本 7.0.0 才开始加入

🚀 Version
This rule was introduced in eslint-plugin-vue v7.0.0

Vue2 项目使用的 eslint-plugin-vue@4.7.1 的文档仅有关于key能否置于<template>上的规则约束。

disallow key attribute on <template> (vue/no-template-key)

该规则从版本 3.4.0 开始加入

🚀 Version
This rule was introduced in eslint-plugin-vue v3.4.0

可见 eslint-plugin-vue@4.7.1 的 vue/no-template-key 约束了 key 的位置,不得放在<template>上。

        旧的 Vue2 项目的 key 并没有放在 <template> 上却报错:<template v-for> key should be placed on the <template> tag.,可以看出是被当成 Vue3 来检查了。

        这个提示属于 eslint-plugin-vue v7.0.0 版本及以上的规范,项目里的 eslint-plugin-vue 版本是 4.7.1,版本 7.0.0 的规范为什么会出现在这,还待查询......

        猜测是由于插件 Volar 未配置支持 Vue2 模板。


问题解决

  1. 禁用插件 Vetur,使用插件 Volar;
    Vue3 文档建议使用 Volar,配置好后就可只使用 Volar 同时开发 Vue2 & Vue3,而使用 Volar 需要禁用 Vuter:

    You need to disable Vetur to avoid conflicts.

  2. 在项目根目录增加文件 jsconfig.json,文件内容如下

    {"vueCompilerOptions": {"experimentalCompatMode": 2},
    }
    

以上解决方法参考的是 Volar 关于 tsconfig.json 的设置

Using
Setup for Vue 2
3.Support Vue 2 template
Volar preferentially supports Vue 3. Vue 3 and Vue 2 template has some different. You need to set the experimentalCompatMode option to support Vue 2 template.

// tsconfig.json
{"compilerOptions": {...},"vueCompilerOptions": {"experimentalCompatMode": 2},
}

        我也不知道为什么这样是成功的,Vuter 文档有提及 jsconfig.json 的配置,Volar 文档仅提及了 tsconfig.json 的配置,就想着试下,就正常了


VS Code 插件地址

        Vuter [Vue2 项目使用的插件]
         [Vue3 项目使用的插件]

http://www.lryc.cn/news/145296.html

相关文章:

  • 启动线程方法 start ()和 run ()有什么区别
  • Java的全排列模板
  • 读书笔记——《万物有灵》
  • 面试现场表现:展示你的编程能力和沟通技巧
  • 34亿的mysql表如何优雅的扩字段长度兵并归档重建
  • C#_进程单例模式.秒懂Mutex
  • AcWing 5050. 排序 (每日一题)
  • 【TypeScript】proxy 和 Reflect
  • STM32f103入门(5)定时器中断
  • Mybatis查询数据
  • 【前车之鉴】: 2023最新教程-将java程序打包到maven私服的正确打开方式,详细流程介绍不怕你掌握不了
  • 如何理解IaaS、PaaS、SaaS?盘点受欢迎的八大SaaS平台!
  • 立创EDA专业版的原理图上器件有一个虚线框
  • JUC并发编程--------基础篇
  • 秒懂算法2
  • 隐秘的角落:Java连接Oracle提示Connection timed out
  • 基于微信小程序的餐厅预订系统的设计与实现(论文+源码)_kaic
  • 科技政策 | 四川省科学技术厅关于发布2024年第一批省级科技计划项目申报指南的通知
  • 深入了解Webpack:特性、特点和结合JS混淆加密的实例
  • 2023-08-23力扣每日一题
  • 分发饼干【贪心算法】
  • 为什么网络互联地址设置为30位地址
  • 青少年棒球锦标赛发展·棒球1号位
  • Unity实现UI图片面板滚动播放效果第二弹
  • Redis的基本操作
  • 省级智慧农业大数据平台项目规划建设方案[195页Word]
  • php图片批量压缩并同时保持清晰度
  • 243:vue+Openlayers 更改鼠标滚轮缩放地图大小,每次缩放小一点
  • NOI2015D. 荷马史诗
  • 并法编程(集合类不安全)03详细讲解未补充