๐ป Lab 5 - Generate a utility lib
May 31, 2023 ยท View on GitHub
โฐ Estimated time: 5-10 minutes
Let's fix the ratings! They don't look that good and they could benefit from some formatting.
We will create a shared utility lib where we'll add our formatters and see how to import them in our components afterwards.
๐ Learning outcomes:
- Get familiar with generating project specific, framework agnostic utility libs
๐ฒ After this workshop, you should have:
App Screenshot
File structure
๐๏ธโโ๏ธ Steps:
-
Stop the
nx serve -
Use the
@nx/jspackage to generate another lib in thelibs/storefolder - let's call itutil-formatters. -
Add the code for the utility function to the new library you just created
libs/store/util-formatters/src/lib/store-util-formatters.ts -
Use it in your frontend project to format the rating for each game
๐ณ Hint
app.component.ts:import { formatRating } from '@bg-hoard/store/util-formatters'; export class AppComponent { //... formatRating = formatRating; }app.component.html:{{ formatRating(game.rating) }}
-
Serve the store app - notice how the ratings are formatted.
-
Launch the project graph - notice how the app depends on two libs now.
-
Inspect what changed from the last time you committed, then commit your changes
๐If you get stuck, check out the solution