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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:57:18  来源:igfitidea点击:

define path in tsconfig.app.json for angular project

angulartypescriptimportangular-clitsconfig

提问by koryakinp

The project was generated via angular CLI. I have the following folder structure:

该项目是通过 angular CLI 生成的。我有以下文件夹结构:

enter image description here

enter image description here

I want to define a path to a barfolder in tsconfig.app.jsonand import Carto Garage.

我想在tsconfig.app.json 中定义一个bar文件夹的路径并导入CarGarage.

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 pathslike that:

你需要这样定义paths

   "paths" : {
      "@bar/*" : [
          "foo/bar/*"
      ]
    },

For more information read

欲了解更多信息,请阅读

回答by Philip John

Please do not forget to put the baseUrlfirst 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

而这个问题貌似重复问题