37

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?

1
  • 2
    With the new version of angular (Versions 6 / 7) you'll be able to generate an angular library within your project directly using the CLI. This should let you not have to worry about NPM publishing at all. Check this Medium article out for more details on how to do this: medium.com/@tomsu/… Commented Nov 11, 2018 at 14:25

2 Answers 2

37

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 this https://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

Sign up to request clarification or add additional context in comments.

3 Comments

first link died
Updated, but checkout yeoman generator, it has better documentation
npmjs link died. Second link password protected. Not much of use left in this answer.
3

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.

2 Comments

Is there any way by which we can create a private repository using Bit?
@SenthilkumarRamasamy Bit let you have 2 private repo and infinite component per repo (so basically unlimited component ;). That said, It may not work the way you expect on project having different angular version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.