Skip to content

Commit 5a3e113

Browse files
committed
add 08-routing-recap lesson
1 parent c4fed99 commit 5a3e113

File tree

15 files changed

+264
-4
lines changed

15 files changed

+264
-4
lines changed

angular.json

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,103 @@
778778
}
779779
}
780780
}
781+
},
782+
"08-routing-recap": {
783+
"projectType": "application",
784+
"schematics": {
785+
"@schematics/angular:component": {
786+
"inlineTemplate": true,
787+
"inlineStyle": true,
788+
"skipTests": true
789+
},
790+
"@schematics/angular:class": {
791+
"skipTests": true
792+
},
793+
"@schematics/angular:directive": {
794+
"skipTests": true
795+
},
796+
"@schematics/angular:guard": {
797+
"skipTests": true
798+
},
799+
"@schematics/angular:interceptor": {
800+
"skipTests": true
801+
},
802+
"@schematics/angular:pipe": {
803+
"skipTests": true
804+
},
805+
"@schematics/angular:resolver": {
806+
"skipTests": true
807+
},
808+
"@schematics/angular:service": {
809+
"skipTests": true
810+
}
811+
},
812+
"root": "projects/08-routing-recap",
813+
"sourceRoot": "projects/08-routing-recap/src",
814+
"prefix": "app",
815+
"architect": {
816+
"build": {
817+
"builder": "@angular-devkit/build-angular:application",
818+
"options": {
819+
"outputPath": "dist/08-routing-recap",
820+
"index": "projects/08-routing-recap/src/index.html",
821+
"browser": "projects/08-routing-recap/src/main.ts",
822+
"polyfills": [
823+
"zone.js"
824+
],
825+
"tsConfig": "projects/08-routing-recap/tsconfig.app.json",
826+
"assets": [
827+
"projects/08-routing-recap/src/favicon.ico",
828+
"projects/08-routing-recap/src/assets"
829+
],
830+
"styles": [
831+
"projects/08-routing-recap/src/styles.css"
832+
],
833+
"scripts": []
834+
},
835+
"configurations": {
836+
"production": {
837+
"budgets": [
838+
{
839+
"type": "initial",
840+
"maximumWarning": "500kb",
841+
"maximumError": "1mb"
842+
},
843+
{
844+
"type": "anyComponentStyle",
845+
"maximumWarning": "2kb",
846+
"maximumError": "4kb"
847+
}
848+
],
849+
"outputHashing": "all"
850+
},
851+
"development": {
852+
"optimization": false,
853+
"extractLicenses": false,
854+
"sourceMap": true
855+
}
856+
},
857+
"defaultConfiguration": "production"
858+
},
859+
"serve": {
860+
"builder": "@angular-devkit/build-angular:dev-server",
861+
"configurations": {
862+
"production": {
863+
"buildTarget": "08-routing-recap:build:production"
864+
},
865+
"development": {
866+
"buildTarget": "08-routing-recap:build:development"
867+
}
868+
},
869+
"defaultConfiguration": "development"
870+
},
871+
"extract-i18n": {
872+
"builder": "@angular-devkit/build-angular:extract-i18n",
873+
"options": {
874+
"buildTarget": "08-routing-recap:build"
875+
}
876+
}
877+
}
781878
}
782879
},
783880
"cli": {

projects/05-control-flow-for/src/app/app.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Component } from '@angular/core';
2-
import { CommonModule } from '@angular/common';
32

43
@Component({
54
selector: 'app-root',
65
standalone: true,
7-
imports: [CommonModule],
86
template: `
97
<section class="container">
108
<!-- This article element represents and entire listing -->

projects/06-input-output/src/app/app.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { Component } from '@angular/core';
2-
import { CommonModule } from '@angular/common';
32
import { Car } from './car ';
43

54
@Component({
65
selector: 'app-root',
76
standalone: true,
8-
imports: [CommonModule],
97
template: `
108
<h1>Saved Cars {{ savedCarList.length }}</h1>
119
<section class="container">

projects/08-routing-recap/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 08-routing-recap
2+
3+
In this activity, you'll learn
4+
5+
- how to leverage the RouterLink directive to make the most use of Angular Router.
6+
- how to get paremeters from the dynamic urls
7+
8+
## Run this example
9+
10+
From the command line at the root of the project:
11+
12+
```bash
13+
ng serve 08-routing-recap
14+
```
15+
16+
## Instructions
17+
18+
### Define Routes & add `<router-outlet />`
19+
20+
1. In `app.routes.ts`, define a route for `HomeComponent` as the default route.
21+
22+
1. Define routes for `DetailsComponent`, add a URL parameter to the path for `DetailComponent` called `id` -
23+
24+
```
25+
/details/:id
26+
```
27+
28+
1. In `app.component.ts`, add the `<router-outlet />` to component template.
29+
30+
### Update components
31+
32+
1. In `app.component.ts`, dynamically generate a list of links for products in `productTitles`.
33+
34+
- Generate a link and use the `routerLink` attribute.
35+
36+
- Use the index of the `productTitles` element
37+
38+
_Hint, you can access ` $index`` in the context of the `@for` [@for documentation](https://angular.dev/api/core/@for)_
39+
40+
1. In, `details.component.ts`, create a property called `productId` of type `number`.
41+
42+
1. In detail component define an `@Input` property called `id` and create `setter` function for it:
43+
44+
```typescript
45+
@Input() set id(value: number) {
46+
this.productId = value;
47+
}
48+
```
49+
50+
1. Update the `DetailsComponent` template to reference the product from the product list that at the indeces for `productId`.
51+
52+
1. Save your changes.
53+
54+
1. Confirm that you can now route between products in the browser.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Component } from '@angular/core';
2+
import { RouterOutlet } from '@angular/router';
3+
4+
@Component({
5+
selector: 'app-root',
6+
standalone: true,
7+
imports: [RouterOutlet],
8+
template: ` <h1>Welcome to {{ title }}!</h1> `,
9+
})
10+
export class AppComponent {
11+
title = '08-routing-recap';
12+
13+
productTitles = ['Product 1', 'Product 2', 'Product 3'];
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ApplicationConfig } from '@angular/core';
2+
import { provideRouter } from '@angular/router';
3+
4+
import { routes } from './app.routes';
5+
6+
export const appConfig: ApplicationConfig = {
7+
providers: [provideRouter(routes)]
8+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Routes } from '@angular/router';
2+
3+
export const routes: Routes = [];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Component } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
4+
@Component({
5+
selector: 'app-details',
6+
standalone: true,
7+
imports: [CommonModule],
8+
template: `
9+
<section>
10+
<p>Title</p>
11+
<ul>
12+
<li>Price</li>
13+
<li>Description</li>
14+
</ul>
15+
</section>
16+
`,
17+
styles: ``,
18+
})
19+
export class DetailsComponent {
20+
productList = [
21+
{
22+
title: 'Product 1',
23+
price: 9234,
24+
description: 'Product 1 is the best',
25+
},
26+
{
27+
title: 'Product 2',
28+
price: 3043,
29+
description: 'Product 2 is special',
30+
},
31+
{
32+
title: 'Product 3',
33+
price: 4355,
34+
description: 'Product 3 has my heart',
35+
},
36+
];
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
4+
@Component({
5+
selector: 'app-home',
6+
standalone: true,
7+
imports: [CommonModule],
8+
template: `
9+
<p>
10+
home works!
11+
</p>
12+
`,
13+
styles: ``
14+
})
15+
export class HomeComponent {
16+
17+
}

projects/08-routing-recap/src/assets/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)