【发布时间】:2023-03-18 17:46:01
【问题描述】:
我正在使用 Angular 11 并尝试使用像 import {smthg} from '@common' 这样的短导入而不是 import {smthg} from '../../../common'
但我总是在 IDEA 中遇到错误:TS2307: Cannot find module '@common' or its corresponding type declarations.
在尝试编译 .ts 文件时控制台出现同样的错误 (ng serve)
有趣的是,当我将/index 添加到导入时,IDEA 停止诅咒,但错误并没有在控制台中消失
myAngularProject
│ package.json
│ tsconfig.json
│ tsconfig.app.json
│ angular.json
│
└───src
│ main.ts
│ index.html
│
└───app
│
└───common
│
└───features
tsconfig.json:
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@common/*": ["app/common/*"],
"@features/*": ["app/features/*"],
"@platform/*": ["app/platform/*"],
"@env": ["environments/environment"]
},
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
tsconfig.app.json:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": ["node"]
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}
版本:
Angular CLI: 11.0.7
Node: 14.2.0
OS: darwin x64
Angular: 11.0.9
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1100.7
@angular-devkit/build-angular 0.1100.7
@angular-devkit/core 11.0.7
@angular-devkit/schematics 11.0.7
@angular/cli 11.0.7
@schematics/angular 11.0.7
@schematics/update 0.1100.7
rxjs 6.6.3
typescript 4.0.5
【问题讨论】:
-
据我所知,Angular 编译器不支持
paths。绝对烦人... -
感谢这个可怕的消息(看起来是真的。好吧,看来唯一的方法是放弃 angular CLI 并使用 webpack。
-
我会在一年前推荐这种方法,但是有太多的文档假定 CLI 以及在它之上构建的工具,例如 Ionic CLI,它是您应该谨慎做出的决定。我并不是说这是错误的,因为我特别讨厌这么多分层的工具抽象,它们不允许您在需要时了解内部结构,但请仔细考虑。祝你好运。
标签: angular typescript tsconfig aliases angular11