- ng new weather –createApplication = false
-
ng generate library weather
It will create & update 3 files
weater-src-lib-public_api.ts
“/lib/” [weather.service, weather.component, weather.module]
- ng generate application weathertest
- In “agngular.json” new two projects added. “Projects” : {weather, weathertest}
- In “package.json” new added “ng.packager” : “4.2.0”
- In “tsconfig.json” added reference library output directory. “path”: {“weather”:dist/weather, “weather/*” : “dist/weather/”}
-
import two modules in “weather.modules.ts”
HttpClientModule, CommonModule
-
Add script in package.json to build our library
“script”:{“weatherbuild”: “ng build weather”}
:> npm run weatherbuild
Output will be in “dist” directory
Test our library within our test application project
-
Inside wethertest folder import our weather module from weather folder remeber the “tsconfig” path entry is helping to resolve “dist” directory.
weathertest-app.module.ts import {weatherModule} from “weather”
- In appcomponent add our component tag with required input
- Test the app with “ng serve weathertest”
3 Ways to use the library in your project
1. Package the library
- Go to library bulder directory “cd dist/weather”
-
Type:> npm pack
Output : a new “taz” file will be created
-
Now we can install library using npm install passing this package URL
“npm install ./dist/weather/weather-0.0.1.tgz”
-
We can see package has been installed using file url in “package.json”.
“weather”:”file:dist/weather/weather-0.0.1.tgz”
- remove path from “tsconfig.json” (“weather”:”dist/…”)
- Build our app : ng buld weathertest
-
run the app with simple http server
dist-weathertest
cd dist/weathertest/
http-server
2. We can publish our library to npm, we can use private npm repo called “verdaccion”