在 tsconfig.json 上使用 path 不会使用 Angular 解析



为了在导入文件时避免相对路径,我尝试配置角度来理解路径。

到目前为止,它根本不起作用,我的代码:

//tsconfig.app.json
"compilerOptions":{
//lot of things . If it matter moduleResultion is set to node
"baseUrl": ".",
"paths": {
"@modules": ["app/modules/"]
...
},
}

//in a component: 
import { X } from '@modules/Y/component/index'

当运行ng serve时,控制台会输出一个错误: Cannot find module '@modules/Y/component/index'.

这肯定适用于像import { X } from ../../../modules/Y/component/index这样的相对路径

所以,我希望我的 tsconfig.app.json 或 tsconfig.json(或两者兼而有之)是错误的,但是,我找不到任何关于如何正确为角度应用程序执行此操作的好教程。

目前使用 angular 4 和基本的关联工具(打字稿 2.3.3、angular-cli 1.0.6 和提供 webpack)

你能向我指出问题,或者给我一个很好的文档/tuto来解决这个问题吗?到目前为止,我在SO或github问题上看到的所有答案

都不起作用。注意:体系结构如下所示

project
|
-tsconfig.json //had try things on this one too but does nothing.
-src/
|
-tsconfig.app.json
-app/
|
-modules
-othersFolder

你的代码应该可以使用

"paths": {
"@modules/*": ["app/modules/*"]
...
}

在此处阅读有关打字稿中模块分辨率的更多信息

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

最新更新