typescript 在 tsconfig.app.json 中为 angular 项目定义路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46638891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
define path in tsconfig.app.json for angular project
提问by koryakinp
The project was generated via angular CLI. I have the following folder structure:
该项目是通过 angular CLI 生成的。我有以下文件夹结构:
I want to define a path to a barfolder in tsconfig.app.jsonand 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”。
回答by Max Koretskyi
You need to define paths
like that:
你需要这样定义paths
:
"paths" : {
"@bar/*" : [
"foo/bar/*"
]
},
For more information read
欲了解更多信息,请阅读
回答by Philip John
Please do not forget to put the baseUrl
first before adding the paths
. I spent hours trying to figure out where I was wrong.
请不要忘记baseUrl
在添加paths
. 我花了几个小时试图找出我错在哪里。
"baseUrl": "./",
"paths": {
...
}
回答by Vignesh
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 duplicateof question
而这个问题貌似重复的问题