如何在多个 Angular 2 项目之间共享一个 Angular 2 组件?

How can I share an Angular 2 component between multiple Angular 2 projects?

理想情况下,我想创建一个独立的 Angular 2 组件(带有测试),然后在两个或三个不同的 Angular 2 站点之间重复使用它。实现这一目标的好方法是什么? 还有一个额外的问题 - 是否存在第 3 方 Angular 2 个组件?

更新

官方 Angular CLI https://angular.io/guide/creating-libraries

您可以创建一个 angular2 组件库(带有测试),然后在任意数量的项目中使用它

然后您可以将其发布到 npm 上以供 public 使用。如果你想保密,可以阅读这篇https://docs.npmjs.com/creating-and-publishing-private-packages

是的,存在许多第 3 方 angular2 组件。这里有一些来自 valor-software https://github.com/valor-software/ng2-plans


如果您不想发布您的库并仍然使用它,您可以创建一个单一的 repo 架构。前谷歌员工有一个名为 nx https://nx.dev/. You can create a library using it and can use it in your project https://nx.dev/latest/angular/tutorial/08-create-libs

的工具

angular 库 https://github.com/jvandemo/generator-angular2-library

的 Yeoman 生成器

这有点晚了,但我想建议最近发布到 public beta for Angular. You can wrap each component inside an ngModule (as done by most ng component libs) and then use bit to share the component between two different projects or apps. The advantages are that you can publish, install and update individual components. bit will handle the packing of the component with all its dependencies, files etc so you can then install it another project using npm/yarn. Also, you can then use bit to import the component's code into the 2nd repo, make changes and sync them back between the repos. You can take a look at the Angular with bit tutorial and guidelines (written together by teams bit and angular). Here are also some examples 的另一个解决方案。希望这可以帮助。我在团队中,所以请随时提出任何问题,如果可以的话,我很乐意提供帮助。