简体   繁体   English

Angular 10:使用绝对路径导入时 VSCode 显示错误

[英]Angular 10: VSCode show error when import with absolute path

after Angular 10 upgrade from Angular 9. VSCode show error when import with absolute path: Angular 10 从 Angular 升级后 9. 使用绝对路径导入时 VSCode 显示错误:

import { IconModule } from 'src/app/shared/components/icon/icon.module';

the error:错误:

Cannot find module 'src/app/shared/components/icon/icon.module'.ts(2307)找不到模块 'src/app/shared/components/icon/icon.module'.ts(2307)

if i change with a relative path it works.如果我用相对路径进行更改,它会起作用。 The same path works if downgrade to Angular 9.如果降级到 Angular 9,则相同的路径有效。

My tsconfig setting:我的 tsconfig 设置:

./tsconfig.json ./tsconfig.json

{
  "files": [],
  "references": [
    {
      "path": "./src/tsconfig.app.json"
    },
    {
      "path": "./src/tsconfig.spec.json"
    },
    {
      "path": "./e2e/tsconfig.e2e.json"
    }
  ]
}

./src/tsconfig.app.json ./src/tsconfig.app.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": []
  },
  "files": [
    "main.ts",
    "polyfills.ts"
  ]
}

./tsconfig.base.json ./tsconfig.base.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": false,
    "target": "es2015",
    "resolveJsonModule": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "jszip": [
        "node_modules/jszip/dist/jszip.min.js"
      ]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictTemplates": true,
    "strictInjectionParameters": true
  },
}

Update VSCode to the latest version (1.46.1) has fixed the issue.将 VSCode 更新到最新版本 (1.46.1) 已解决此问题。

Seems, the problem happens because of your tsconfig files location.似乎,问题是由于您的tsconfig文件位置而发生的。 It located in src folder already, and "baseUrl": "./" linked to that folder.它已经位于src文件夹中,并且"baseUrl": "./"链接到该文件夹。 So in your absolute path no need to write src folder.所以在你的绝对路径中不需要写src文件夹。 Just start from app .只需从app开始。 Like 'app/shared/components/icon/icon.module' .'app/shared/components/icon/icon.module'

Or move your tsconfig files up to root.或者将您的tsconfig文件移至根目录。

It might be an issue caused because of opening a sub-folder in VSCode where import consists of absolute paths and those absolute paths starts from a parent folder of your currently opened folder.这可能是由于在 VSCode 中打开一个子文件夹而导致的问题,其中导入由绝对路径组成,而这些绝对路径从您当前打开的文件夹的父文件夹开始。

Fix: Open your project's root directory in VSCode修复:在 VSCode 中打开项目的根目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM