简体   繁体   English

在 tsconfig.app.json 中为 angular 项目定义路径

[英]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.

相关问题 使用tsconfig.app.json - Use of tsconfig.app.json ENOENT:在使用 angular 4 的 tsconfig.app.json 中没有这样的文件或目录? - ENOENT: no such file or directory in tsconfig.app.json using angular 4? tsconfig.app.json 的别名不起作用 Angular 11 - Aliasing for tsconfig.app.json not working Angular 11 Angular 9 tsconfig.json & tsconfig.app.json 路径未按预期工作 - Angular 9 tsconfig.json & tsconfig.app.json paths are not working as expected Angular 中 tsconfig.json 和 tsconfig.app.json 文件的区别 - Difference between tsconfig.json and tsconfig.app.json files in Angular 更改tsconfig.app.json的位置 - Change the location of tsconfig.app.json 路径在 tsconfig.app.json 中没有按预期工作 - paths is not working in tsconfig.app.json as expected tsconfig.app.json中的baseUrl不会覆盖tsconfig.json中的baseUrl - baseUrl in tsconfig.app.json is not overiding the baseUrl in tsconfig.json 部署到App Engine时找不到tsconfig.app.json - tsconfig.app.json not found when deploying to App Engine 角度5:在安装aws-sdk时,在tsconfig.app.json中删除“类型”:[]或更改为“类型”:[“节点”]后,IE无法工作 - Angular 5: IE not working after removing “types”: [ ] or changing to “types”: [“node”] in tsconfig.app.json during installing of aws-sdk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM