๐ป Lab 8 - Displaying a full game in the routed game-detail component
October 16, 2022 ยท View on GitHub
โฐ Estimated time: 15-20 minutes
Now we have a proper API that we can use to make HTTP requests. We'll look at how the Nrwl NestJS generators created a helpful proxy configuration for us.
๐ Learning outcomes:
- Learn how to connect frontend and backend apps in an Nx workspace
๐ฒ After this workshop, you should have:
App screenshot
๐๏ธโโ๏ธ Steps:
-
Import the
HttpClientModuleinapps/store/src/app/app.module.tsand add it to the module's imports array:๐ณ Hint
import { HttpClientModule } from '@angular/common/http';
-
Within the same folder, inject the
HttpClientin the app.component.ts's constructor and call your new API as an HTTP requestโ ๏ธ Notice how we assume it will be available at
/api(more on that below) -
Because our list of
gamesis now an Observable, we need to add anasyncpipe in the template that gets the games:๐ณ Hint
<mat-card class="game-card" *ngFor="let game of games | async" <--HERE [routerLink]="['/game', game.id]" >...</mat-card >
-
Run
nx serve apiโ ๏ธ Notice the PORT number
-
In a different tab, run
nx serve storeโ ๏ธ Again, notice the PORT number
-
Everything should still look/function the same!
๐ You can inspect your Network tab in the dev tools and notice an XHR request made to
http://localhost:4200/api/games
๐ Even though the frontend and server are being exposed at different ports, we can call /api from the frontend store because Nx created a proxy configuration for us (see apps/store/proxy.conf.json) so any calls to /api are being routed to the correct address/port where the API is running.
This helps you avoid CORS issues while developing locally.
Now let's load the full game in our routed component!
-
Inside the
libs/store/feature-game-detail/src/libfolder, replace the following files:/game-detail/game-detail.component.ts / html- /store-feature-game-detail.module.ts
โ ๏ธ Notice how we're using the shared
formatRating()function in our routed component as well! -
You should now see a fully-fleshed out detail component when you link on the card list at the top! (you might need to restart your
nx serve storeso the new button styles can be copied over) -
Inspect what changed from the last time you committed, then commit your changes
๐If you get stuck, check out the solution