๐ป Lab 6 - Generate a route lib
May 31, 2023 ยท View on GitHub
โฐ Estimated time: 15-25 minutes
We'll look at more advanced usages of the @nx/angular generators and generate a new route lib for our store application. We'll see how Nx takes care of most of the work, and we just have to do the wiring up!
๐ Learning outcomes:
- Get familiar with more advanced usages of Nx generators to create an Angular route lib
๐ฒ After this workshop, you should have:
App Screenshot
File structure
๐๏ธโโ๏ธ Steps:
-
Stop
nx serve -
Use the
@nx/angular:libgenerator to generate a new routing library calledfeature-game-detailthat:- lives under
libs/store - has lazy loading
- has routing enabled
- its parent routing module is
apps/store/src/app/app.module.ts
โ ๏ธ Use
--helpwith the above generator to figure out which options you need to use to enable all the above (See the solution if still unsure) โ ๏ธ You may need to remove the file atapps/store/src/app/app.routing.tsand update theapp.module.tsfile with an empty array. - lives under
-
Generate a new Angular component called
game-detailunder the above lib you created -
Change the routing path in
apps/store/src/app/app.module.tsto pick up the game ID from the URL๐ณ Hint
{ path: 'game/:id', // <---- HERE loadChildren: () => import('@bg-hoard/store/feature-game-detail').then(/* ... */) }
-
Adjust your routes in
libs/store/feature-game-detail/src/lib/lib.routes.tsand make sure it's pointing to thegame-detailcomponent you generated above๐ณ Hint
import { Route } from '@angular/router';
import { GameDetailComponent } from './game-detail/game-detail.component';
export const storeFeatureGameDetailRoutes: Route[] = [
{ path: '', pathMatch: 'full', component: GameDetailComponent },
];
</details>
-
Import
MatCardModuleinstore-feature-game-detail.module.tsand add it to the module'simports: [...]:๐ณ Hint
import { MatCardModule } from '@angular/material/card';
-
Populate your new component with the provided files:
game-detail.component.ts / css / html -
We now need to display your new routed component. Let's add a
<router-outlet>below our list of cards:๐ณ Hint
apps/store/src/app/app.component.html:<div class="container"> <div class="games-layout"> <mat-card class="game-card" *ngFor="let game of games"> ... </mat-card> </div> <router-outlet></router-outlet> <--- ADD IT HERE </div>
-
Make clicking on each card route to the
feature-game-detailmodule with the game's ID:๐ณ Hint
<div class="container"> <div class="games-layout"> <mat-card class="game-card" *ngFor="let game of games" [routerLink]="['/game', game.id]" > <--- HERE ... </mat-card> </div> <router-outlet></router-outlet> </div>
-
Serve your app again, click on some games, and compare with the screenshot above
-
Launch the project graph and see what's been added
-
Inspect what changed from the last time you committed, then commit your changes
The result is still pretty simple though. Our route just displays the ID of the selected game in a card. It would be great if we had some API to get the full game from that ID!
๐If you get stuck, check out the solution