๐ป Lab 4 - Generate a component lib
August 29, 2022 ยท View on GitHub
โฐ Estimated time: 10 minutes
Let's add a header to our app! Because headers can be shared with other components, we will create a common lib that others can import as well.
๐ Learning outcomes:
- Get familiar with generating project specific component libraries inside a folder
๐ฒ After this workshop, you should have:
App Screenshot
File structure
๐๏ธโโ๏ธ Steps:
-
Stop the
nx serve
-
Generate a new Angular library called
ui-sharedin thelibs/storefolder๐ณ Hint
- it's a generator! you've used it before in the second lab, but instead of an
app, we now want to generate alib - use the
--helpcommand to figure out how to generate it in a directory
- it's a generator! you've used it before in the second lab, but instead of an
-
Generate a new Angular component, called
header, inside the lib you just createdโ ๏ธ Play around with the generator options so that the generated component is automatically exported from the lib's module
๐ณ Hint
use
--helpto figure out how to specify under which project you want to generate the new component and how to automatically have it exported
-
Import
MatToolbarModulein the new shared module you just created๐ณ Hint
import { MatToolbarModule } from '@angular/material/toolbar'; @NgModule({ imports: [CommonModule, MatToolbarModule], //...
-
Import the
StoreUiSharedModuleyou just created in theapps/store/src/app/app.module.ts๐ณ Hint
import { StoreUiSharedModule } from '@bg-hoard/store/ui-shared';โ ๏ธ You might need to restart the TS compiler in your editor (
CTRL+SHIFT+Pin VSCode and search forRestart Typescript)
-
Let's use the new shared header component we created
- Add your new component to
apps/store/src/app/app.component.html
๐ณ Hint
<bg-hoard-header title="Board Game Hoard"></bg-hoard-header> <!-- right at the top - above our container --> <div class="container"></div>
- Add your new component to
-
Serve the project and test the changes
-
Run the command to inspect the project graph - What do you see? (Remember to "Select all" in the top left corner)
๐ณ Hint
nx graph
-
Inspect what changed from the last time you committed, then commit your changes
๐If you get stuck, check out the solution