Angular 6 / declare path for library in tsconfig.lib.json

笑着哭i 提交于 2020-05-15 02:16:08

问题


I'm trying to configure the paths for an Angular 6 library (I have successfully configured the paths for a previous Angular project)

Here is what is working for my previous app in the tsconfig.json file:

"compilerOptions": {
    [...]
    "baseUrl": "src",
    "paths": {
      "@class/*": [ "app/class/*" ],
      "@services/*": [ "app/services/*" ],
      "@views/*": [ "app/views/*" ]
    }
}

and then use it like for example:

Import { Item } form '@class/item';

In my new app I'm trying the same way in the tsconfig.**lib**.json file:

"compilerOptions": {
    [...]
    "baseUrl": "src",
    "paths": {
      "@class/*": [ "lib/class/*" ],
      "@services/*": [ "lib/services/*" ],
      "@views/*": [ "lib/views/*" ]
    }
}

I have tried to import a class in a component for my library like this, but it does not work (VSCode cannot find the file):

Import { Item } form '@class/item';

Note that the import statement in the main project is working:

Import { Item } from 'myLibrary';

Any idea of what I'm not doing well ?

Thanks for your help.


回答1:


This is from my above comment, if you see that this is a correct answer.

Angular will use the tsconfig.json file, which is configured in the angular.json file in **architect --> build --> options --> tsconfig**. Whereas VS Code uses the tsconfig.json file, which is at the bottom of the workspace. You need to add the paths in both configurations to get both of them to work correctly, or change the project to use the base tsconfig.json file in the angular.json file.

The tsconfig.json file has a property that extends which will take another file as base and override everything which are declared in the tsconfig.lib.json file. So if you have paths declared in the tsconfig.lib.json file, then the paths are no longer needed in the tsconfig.json file.

EDIT 29.7.2019 tsconfig.json

"baseUrl": "src",
"paths": {
  "@ProjectName1/*": ["../projects/ProjectName1/src/app/modules/*"],
  "@Projectname2/*": ["../projects/Projectname2/src/app/modules/*"],
  "@environment/*": ["environments/*"],
  "project-core": ["../dist/project/project-core"],
  "project-core/*": ["../dist/project/project-core/*"]
}

And currenlty I dont't have paths declared in tsconfig.app.json because it would be same as in tsconfig.json. So this is different from my earlier answer, because I have paths only in tsconfig.json.



来源:https://stackoverflow.com/questions/52147201/angular-6-declare-path-for-library-in-tsconfig-lib-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!