简体   繁体   中英

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

Ideally, I'd like to create a stand-alone Angular 2 component (with tests) and then re-use it between two or three different Angular 2 sites. What are the good ways to achieve this? And a bonus question - do any 3rd party Angular 2 components exist?

UPDATE

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

You can create an angular2 component library (with tests) and then use it in any number of projects

Then you can publish it on npm for public use. If you want to keep it private, you can read thishttps://docs.npmjs.com/creating-and-publishing-private-packages

Yes, many 3rd party angular2 components exist. Here are a few from valor-software https://github.com/valor-software/ng2-plans


If you don't want to publish your library and still use it, you can create a mono repo architecture. There is a tool by ex-googlers called 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


Yeoman Generator for angular library https://github.com/jvandemo/generator-angular2-library

this is a bit late but I'd like to suggest another solution, recently released to 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 . Hope this helps. I'm on team bit so please feel free to ask anything and I'd love to help if I can.

I know this is maybe to late, but it can help you.
I start using Angular and I have the same question, so I tried bit.dev , and it was very cool.
Finally, I created my first library and share all the components to bit.dev collection.
Check out https://bit.dev/joshk/ng-spinners

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM