Vue Vscode <template>下报错 ‘TypeScript intellisense is disabled on template‘

报错信息

在这里插入图片描述

TypeScript intellisense is disabled on template. To enable, configure `"jsx": "preserve"` in the
`"compilerOptions"` property of tsconfig or jsconfig. To disable this prompt instead, configure
`"experimentalDisableTemplateSupport": true` in `"vueCompilerOptions"` property.volar

在线翻译

模板上已禁用TypeScript智能感知。
要启用,请在tsconfig或jsconfig的“compilerOptions”属性中配置“jsx”:“preserve”。
要禁用此提示,请配置“experimentalDisableTemplateSupport”:truein“vueCompilerOptions”`property.volar

报错原因

vscode升级造成的插件不完美契合

解决方法

  1. 找到 jsconfig.json文件
    在这里插入图片描述

  2. 在compilerOptions中加入"jsx": “preserve”,

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "baseUrl": "./",
    "moduleResolution": "node",
    // 添加位置
    "jsx": "preserve",
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  }
}
### 解决方案 在 VSCode 中开发基于 Vite 的 Vue 单文件组件(SFC)项目时,如果遇到缺少 `template` 或 `script` 标签的报错提示,通常是因为 ESLint 配置不正确或者未适配 Vue SFC 的特定规则。以下是详细的解决方案: #### 1. 安装必要的依赖 确保安装了支持 VueTypeScript 的 ESLint 插件以及 Prettier 工具。 ```bash npm install --save-dev eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier eslint-config-prettier eslint-plugin-prettier ``` 此命令会安装以下工具及其插件[^2]: - **eslint**: 主要用于代码质量检测。 - **eslint-plugin-vue**: 提供针对 Vue 组件的支持。 - **@typescript-eslint/parser** 和 **@typescript-eslint/eslint-plugin**: 支持 TypeScript 文件的解析和校验。 - **prettier**, **eslint-config-prettier**, **eslint-plugin-prettier**: 确保格式化工具与 ESLint 不冲突。 --- #### 2. 配置 `.eslintrc.cjs` 创建或更新项目的 `.eslintrc.cjs` 文件以适应 Vue SFC 结构的要求。 ```javascript module.exports = { root: true, env: { node: true, browser: true, // 添加浏览器环境支持 }, extends: [ 'plugin:vue/vue3-recommended', // 使用 Vue 3 推荐配置 'eslint:recommended', '@vue/typescript/recommended', // 同时启用 TypeScript 规则 'plugin:@typescript-eslint/recommended', 'prettier', // 关闭可能与 Prettier 冲突的规则 ], parserOptions: { ecmaVersion: 2020, sourceType: 'module', }, rules: { 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'vue/multi-word-component-names': 'off', // 如果允许单单词命名可以关闭该规则 'vue/no-missing-template': 'error', // 明确要求存在模板部分 'vue/require-script-in-components': 'error', // 要求脚本部分的存在 }, }; ``` 上述配置通过设置 `'vue/no-missing-template'` 和 `'vue/require-script-in-components'` 来强制检查是否存在 `<template>` 和 `<script>` 标签[^2]。 --- #### 3. 更新 VSCode 设置 为了使 VSCode 更好地集成 ESLint 并自动修复错误,在工作区的 `settings.json` 文件中添加如下内容: ```json { "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.validate": ["javascript", "javascriptreact", "typescript", "vue"], "vetur.validation.template": false, // 如果使用 Vetur,则禁用其内置验证以防重复警告 } ``` 注意:如果你正在使用新的官方推荐扩展——Volar 替代 Vetur,请确认已卸载后者并激活前者的功能集。 --- #### 4. 创建基础 Vue SFC 示例 按照标准结构编写一个简单的 Vue 单文件组件来测试配置效果: ```html <template> <div class="example">Hello from Component</div> </template> <script lang="ts"> export default { name: "ExampleComponent", }; </script> <style scoped> .example { color: blue; } </style> ``` 运行 `npx eslint . --ext .vue,.js,.jsx,.ts,.tsx` 命令手动触发静态分析,观察是否有预期中的错误报告被修正。 --- ### 总结 以上方法能够有效解决因 ESLint 配置不当而导致的关于缺失 `template` 或者 `script` 标签的问题。同时建议开发者定期审查自己的 linting 流程是否符合团队编码规范需求,并适时调整相关参数设定。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值