在tsconfig.json上使用path不能使用angular解决

ssb*_*sbb 3 typescript webpack angular-cli angular

为了避免导入文件时出现相对路径,我尝试配置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'
Run Code Online (Sandbox Code Playgroud)

运行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)

您能向我指出问题,还是请给我一个好的文档/教程,请用angular解决这个问题?到目前为止,我在SO或github问题上看到的所有答案都根本不起作用。

注意:体系结构看起来像这样

project
   |
   -tsconfig.json //had try things on this one too but does nothing.
   -src/
     |
     -tsconfig.app.json
     -app/
       |
       -modules
       -othersFolder
Run Code Online (Sandbox Code Playgroud)

yur*_*zui 5

您的代码应与

"paths": {
  "@modules/*": ["app/modules/*"]
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

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