在tsconfig.app.json中为角度项目定义路径
问题描述:
该项目是通过角度CLI生成的。我有以下文件夹结构:在tsconfig.app.json中为角度项目定义路径
我想在tsconfig.app.json定义为酒吧文件夹的路径,并导入到Car
Garage
。
我tsconfig.app.json:
{
"extends": "../tsconfig.json",
"compilerOptions": {
...
"baseUrl": "./",
"paths" : {
"@bar" : ["foo/bar"]
},
...
},
...
}
我Garage.ts:
import { Car } from "@bar/Car";
export class Garage {}
在garage.ts我有一个错误:
Cannot find module '@bar/car'.
答
您需要定义paths
这样的:
"paths" : {
"@bar/*" : [
"foo/bar/*"
]
},
欲了解更多信息,请阅读
答
我认为这可能工作
{
"extends": "../tsconfig.json",
"compilerOptions": {
...
"baseUrl": "./",
"paths" : {
"@bar" : ["foo/bar"],
"@environment/*": ["environments/*"],
"@shared/*": ["app/_shared/*"],
"@helpers/*": ["helpers/*"]
//you can define multiple paths inside this
},
...
},
...
}
,问题看起来像重复question的
你会发现[这篇文章](https://blog.angularindepth.com/configuring-typescript-compiler-a84ed8f87e3#06ea)有帮助,它解释了如何以及为什么使用'路径'相当精心 –
@ AngularInDepth.com , 谢谢你,先生。我简要介绍了这篇文章,但仍然没有得到它。您能否提供一些关于为什么在该特定示例中无法解决“Car”模块的见解? – koryakinp
我已发布[我的回答](https://*.com/a/46640069/2545680) –