Angular 6/在tsconfig.lib.json中声明库的路径 [英] Angular 6 / declare path for library in tsconfig.lib.json

查看:266
本文介绍了Angular 6/在tsconfig.lib.json中声明库的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置Angular 6库的路径(我已经成功配置了上一个Angular项目的路径)

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

以下是tsconfig.json文件中适用于我以前的应用程序的内容:

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/*" ]
    }
}

然后使用它,例如:

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

在我的新应用中,我正在tsconfig.**lib**.json文件中尝试相同的方式:

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/*" ]
    }
}

我已经尝试像这样在我的库的组件中导入一个类,但是它不起作用(VSCode无法找到该文件):

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';

请注意,主项目中的import语句正在工作:

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

Import { Item } from 'myLibrary';

有什么我做得不好的主意吗?

Any idea of what I'm not doing well ?

感谢您的帮助.

推荐答案

这是我上面的评论,如果您看到这是正确的答案.

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

Angular将使用tsconfig.json文件,该文件在**architect --> build --> options --> tsconfig**中的angular.json文件中配置.而VS Code使用tsconfig.json文件,该文件位于工作区的底部.您需要在两种配置中都添加路径,以使它们都能正常工作,或者将项目更改为使用angular.json文件中的基础tsconfig.json文件.

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.

tsconfig.json文件具有扩展的属性,该属性将以另一个文件为基础,并覆盖在tsconfig.lib.json文件中声明的所有内容.因此,如果您在tsconfig.lib.json文件中声明了路径,则tsconfig.json文件中不再需要这些路径.

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.

编辑29.7.2019 tsconfig.json

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/*"]
}

目前,我在tsconfig.app.json中没有声明路径,因为它与tsconfig.json中的路径相同.因此,这与我先前的答案不同,因为我仅在tsconfig.json中具有路径.

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.

这篇关于Angular 6/在tsconfig.lib.json中声明库的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆