简体   繁体   English

Angular CLI 6:如何在主应用程序中使用库子项目?

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

相关问题 Phonegap 3.0,Cordova CLI:在创建ios子项目期间发生错误 - Phonegap 3.0,Cordova CLI: an error occurred during creation of ios sub-project Angular2 Cli项目中的PouchDB身份验证库 - PouchDB Authentication library in Angular2 Cli project 如何在angular cli应用程序中使用typescript声明(.ts.d文件)来使用第三方javascript API / SDK? - How Do I use typescript declarations (.ts.d files) to consume a third party javascript API / SDK in an angular cli application? 消费angular 2自定义库 - Consume angular 2 custom library 如何使用Angular CLI将Angular框架加载到应用程序中? - How is the Angular framework loaded into an application with Angular CLI? 如何更改 Angular CLI 项目中的 Autoprefixer 选项? - How to change Autoprefixer options in Angular CLI project? 如何在库中使用 angular 应用程序的库 - How to use a library of an angular application within a library 使用SystemJS将ng2-ace库集成到新创建的angular-cli(angular2)项目中 - Integrate the ng2-ace library into a freshly created angular-cli (angular2) project using SystemJS 如何将外部库包含到单元测试中:: Angular2-CLI - How to include external library into unit tests::Angular2-CLI 如何让我的本地 JavaScript 项目使用本地库? - How do I make my local JavaScript project consume a local library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM