This project was generated with Angular CLI version 6.1.4.
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Many of the improvements on Angular 6 were to the Angular CLI. The one I have really been looking forward to is the integration of the Angular CLI with ng-packagr to generate and build Angular libraries. In this project we will walk through the details of creating an Angular library.
Angular workspace has two projects:
This is the library of components and services that we want to provide. This is the code that we could publish to npm for example.
This will be a test harness for our library. Sometimes this application is used as documentation and example usage of the library.
*** There will also be a third project for End to End testing that the Angular CLI generates for us by default which we will ignore.
- ALWAYS: Create your workspace using the name of your library-app. Then rename it to the name of your library.
- ALWAYS: Use a prefix when generating a library >>> ng generate library example-ng6-lib --prefix=enl
- Before using our newly generated library you need to build it >>> ng build --prod example-ng6-lib
- ALWAYS: Use the--prod flag when building your library.
- ALWAYS: In your test application import using your library by name and NOT the individual files >>> import { ExampleNg6LibModule } from 'example-ng6-lib';
- ALWAYS: Rebuild your library after making changes.
When generating a component for our library we use the --project flag to tell the Angular CLI that we want the component generated in our library project. command >>> ng generate component foo --project=example-ng6-lib
FOR COMPONENTS: Using export makes the element visible. Adding it to the entry file makes the class visible.
- Never directly modify the library distribution package.json.
- ALWAYS: Use npm pack to create the tgz file.
- Check scripts on root package.json >>> To build & package our library we can now use: npm run package.
Create a test workspace so you can use the library in that application. To use the library in the new application >>> npm install ../example-ng6-lib/dist/example-ng6-lib/example-ng6-lib-0.0.1.tgz
- ALWAYS: Install the library’s .tgz package and NOT the directory.
- In order to actually use a component from the library we need to add the library’s module to our App Module. To do this we make two changes in: src\app\app.module.ts 2.1. Import the ExampleNg6LibModule >>> import { ExampleNg6LibModule } from 'example-ng6-lib'; 2.2. Add the ExampleNg6LibModule to the imports array in our AppModule.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI README.