[英]define path in tsconfig.app.json for angular project
The project was generated via angular CLI.该项目是通过 angular CLI 生成的。 I have the following folder structure:我有以下文件夹结构:
I want to define a path to a bar folder in tsconfig.app.json and import Car
to Garage
.我想在tsconfig.app.json 中定义一个bar文件夹的路径并将Car
导入Garage
。
My tsconfig.app.json :我的tsconfig.app.json :
{
"extends": "../tsconfig.json",
"compilerOptions": {
...
"baseUrl": "./",
"paths" : {
"@bar" : ["foo/bar"]
},
...
},
...
}
My Garage.ts :我的Garage.ts :
import { Car } from "@bar/Car";
export class Garage {}
In garage.ts I have an error:在garage.ts 我有一个错误:
Cannot find module '@bar/car'.找不到模块“@bar/car”。
You need to define paths
like that:您需要定义这样的paths
:
"paths" : {
"@bar/*" : [
"foo/bar/*"
]
},
For more information read欲了解更多信息,请阅读
Please do not forget to put the baseUrl
first before adding the paths
.请不要忘记在添加paths
之前先放置baseUrl
。 I spent hours trying to figure out where I was wrong.我花了几个小时试图找出我错在哪里。
"baseUrl": "./",
"paths": {
...
}
I think this might work我认为这可能有效
{
"extends": "../tsconfig.json",
"compilerOptions": {
...
"baseUrl": "./",
"paths" : {
"@bar" : ["foo/bar"],
"@environment/*": ["environments/*"],
"@shared/*": ["app/_shared/*"],
"@helpers/*": ["helpers/*"]
//you can define multiple paths inside this
},
...
},
...
}
and the question looks like duplicate of question这个问题看起来像问题的重复
在 VS 代码中停止抱怨:CMD + SHIFT + P 然后输入 reload 或 Reload Project 来停止 webstorm 中的抱怨:使缓存失效(文件 | 使缓存失效,失效并重新启动)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.