![](/img/trans.png)
[英]Angular Library paths alias declared in tsconfig.json not working: Module not found: Error: Can't resolve
[英]using path on tsconfig.json doesn't resolve using 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 应用程序正确执行此操作的好的教程。
目前使用 angular 4 和基本的关联工具(typescript 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
您的代码应该与
"paths": {
"@modules/*": ["app/modules/*"]
...
}
在此处阅读有关打字稿中模块分辨率的更多信息
https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
@yurzui ...您的解决方案无效。
我的tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"importHelpers": true,
"module": "esnext",
"moduleResolution": "node",
"paths": {
"@app/*": ["app/*"],
"@environments/*": ["environments/*"],
"@interfaces/*": ["models/interfaces/*"]
},
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
我的导入:
import { Environment } from '@environments/environment';
结果:
ERROR in ./src/app/login/login.component.ts
Module not found: Error: Can't resolve '@environments/environment'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.