tsconfig上使用路径。json不使用角

时间:2021-04-25 23:37:52

In order to avoid relative path when I import file, I try to configure angular to understand path.

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

So far, it's not working at all, my code :

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

//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'

when running ng serve , the console output an error about : : Cannot find module '@modules/Y/component/index'.

运行ng服务时,控制台输出一个错误::无法找到模块‘@modules/Y/组件/索引’。

This is definitly working with relative path like import { X } from ../../../modules/Y/component/index

这显然是在处理相对路径,如从.. ../../模块/Y/组件/索引中导入{X}

So, I expect my tsconfig.app.json or tsconfig.json ( or maybe both) are wrong, however, I can't find any good tutorial about how to do it correctly for an angular app.

我希望我的tsconfig。app。json或tsconfig。json(或者两者都是)是错误的,但是,我找不到任何关于如何正确地为一个角应用程序做正确的教程。

Currently using angular 4 with basic associate tools (typescript 2.3.3, angular-cli 1.0.6 and provide webpack)

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

Can you point out the problem to me, or give me a good doc/tuto that resolve this issue with angular please ? All the answer I have seen so far on SO or github issue didn't work at all.

你能不能给我指出问题所在,或者给我一个好的医生/医生来解决这个问题?到目前为止,我在so或github上看到的所有答案都不管用。

NOTE : The architecture looks like this

注意:架构是这样的。

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

1 个解决方案

#1


2  

Your code should work with

您的代码应该可以使用

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

Read more about module resolution in typescript here

在这里阅读更多关于打字稿中的模块解析

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

https://www.typescriptlang.org/docs/handbook/module-resolution.html路径映射

#1


2  

Your code should work with

您的代码应该可以使用

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

Read more about module resolution in typescript here

在这里阅读更多关于打字稿中的模块解析

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

https://www.typescriptlang.org/docs/handbook/module-resolution.html路径映射