[英]Angular CLI 6: How to consume a library sub-project in the main application?
I've got an Angular 6 project that's made of up a main application, and a separate sub-project that is a library.我有一个 Angular 6 项目,它由一个主应用程序和一个单独的子项目组成,它是一个库。 I'm trying to consume this library from the main application, and I can't seem to get it working.
我试图从主应用程序使用这个库,但我似乎无法让它工作。
In the tsconfig.json
, I have the following paths
configuration:在
tsconfig.json
,我有以下paths
配置:
"paths": {
"@my-company/my-package/*": "dist/my-package/*"
}
And then in the main app, I import the library like so:然后在主应用程序中,我像这样导入库:
import { ButtonModule } from '@my-company/my-package/button';
However, when I build the main application, I get tons of errors about not being able to find modules.然而,当我构建主应用程序时,我收到了大量关于无法找到模块的错误。 For the above import statement, I'll get this error:
对于上面的导入语句,我会得到这个错误:
Module not found: Error: Can't resolve '/Users/jattardi/code/myproject/dist/my-package/button'
However, if I check the dist/my-package
directory, there certainly is a button
directory containing the type definitions.但是,如果我检查
dist/my-package
目录,肯定有一个包含类型定义的button
目录。
The reason my imports have subpaths, eg @my-company/my-package/button
instead of just @my-company/my-package
is to make it tree-shakeable.我的导入有子路径的原因,例如
@my-company/my-package/button
而不是@my-company/my-package
是为了使其可摇树。 Not sure this is possible.不确定这是可能的。 Since this is an Angular 6/ng-packagr generated build, do I lose this ability?
由于这是一个 Angular 6/ng-packagr 生成的构建,我会失去这种能力吗?
I was hit with the same error when trying to consume in the main application.尝试在主应用程序中使用时,我遇到了同样的错误。 My answer will not solve that case as I am trying to get my lazy loaded module, I moved into the new Angular6 library, to work.
我的答案不会解决这种情况,因为我试图让我的懒加载模块,我搬到了新的 Angular6 库中工作。
I was able to publish my library project to npm and consume it in the main application as an npm dependency.我能够将我的库项目发布到 npm 并将其作为 npm 依赖项在主应用程序中使用。 For eg: @santony/ngx-material.
例如:@santony/ngx-material。 May be as a workaround you could do that until this is resolved.
可能作为一种解决方法,您可以这样做,直到解决此问题。
For those who are still facing this problem, there's a "how to" at ng-packagr
's documentation:对于那些仍然面临这个问题的人,
ng-packagr
的文档中有一个“如何”:
https://github.com/ng-packagr/ng-packagr/blob/master/docs/secondary-entrypoints.md https://github.com/ng-packagr/ng-packagr/blob/master/docs/secondary-entrypoints.md
Basically,基本上,
All you have to do is create a
package.json
file and put it where you want a secondary entry point to be created.您所要做的就是创建一个
package.json
文件并将其放在您希望创建辅助入口点的位置。 The contents ofmy_package/testing/package.json
can be as simple as:my_package/testing/package.json
的内容可以很简单:
{
"ngPackage": {}
}
And also, in dev application, I've added the path reference to the secondary endpoint, so it worked:而且,在开发应用程序中,我已经添加了对辅助端点的路径引用,所以它起作用了:
"@my-company/my-library/another": [
"dist/my-library/another"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.