我在 Angular 工作区中有两个项目 - app-x
和 app-z
,我正在尝试缩短导入路径。我在 SO 上遇到了几个相关的帖子,但找不到我正在寻找我的方案的解决方案。
我在两个项目中都有一个 _core
文件夹,并且我有如下导入 -
import { ISomething } from 'projects/app-x/src/app/_core/infrastructure/interfaces';
// or relative equivqlent -
// import { ISomething } from '../../../_core/infrastructure/Interfaces';
import { ISomethingElse } from 'projects/app-z/src/app/_core/infrastructure/interfaces';
// or relative equivqlent -
// import { ISomethingElse } from '../../../_core/infrastructure/Interfaces';
我希望能够将它们写成 -
import { ISomething } from '@core/infrastructure/interfaces';
// and -
import { ISomethingElse } from '@core/infrastructure/interfaces';
在他们各自的项目中。
以下是我如何尝试在为此项目生成的 app-x
文件中配置项目 tsconfig.app.json
的路径 -
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/app",
"types": [],
"paths": {
"@core/*": [
"projects/app-x/src/app/_core/*"
]
}
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}
但它不起作用,VS Code 显示红色波浪线 -
<块引用>找不到模块“@core/infrastructure/Interfaces”或其 对应的类型声明.ts(2307)
This answer here 建议将路径配置添加到工作区根目录的 tsconfig.json
文件中,而不是添加到 tsconfig.app.json
文件中。我知道这可行,但后来我无法在 @core
项目中使用 app-z
,因为它指向 _core
项目的 app-x
文件夹。
这就是我尝试将配置放入相应 tsconfig.app.json
文件的原因。我用几个值调整了 paths
选项,还添加了一个 baseUrl
选项(即使一个存在于根 tsconfig.json
中)并尝试了多个值组合不同的 {{1} } 值。但是失败了。
任何帮助,提示/建议,将不胜感激。谢谢