Add In-App Navigation With Routing
Add In-App Navigation With Routing
io/tutorial/toh-pt5
angular.io
19-24 minutos
1 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
2 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
@NgModule({
imports: [
CommonModule
],
declarations: []
3 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
})
export class AppRoutingModule { }
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
4 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
})
export class AppRoutingModule { }
Routes
The next part of the file is where you configure your routes. Routes tell
the Router which view to display when a user clicks a link or pastes a
URL into the browser address bar.
Since AppRoutingModule already imports HeroesComponent, you
can use it in the routes array:
5 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
src/app/app-routing.module.ts
RouterModule.forRoot()
The @NgModule metadata initializes the router and starts it listening for
browser location changes.
6 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
7 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
Add RouterOutlet
<h1>{{title}}</h1>
<router-outlet></router-outlet>
<app-messages></app-messages>
8 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
Try it
ng serve
The browser should refresh and display the app title but not the list of
heroes.
Look at the browser's address bar. The URL ends in /. The route path to
HeroesComponent is /heroes.
Append /heroes to the URL in the browser address bar. You should
see the familiar heroes master/detail view.
9 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
Ideally, users should be able to click a link to navigate rather than pasting
a route URL into the address bar.
Add a <nav> element and, within that, an anchor element that, when
clicked, triggers navigation to the HeroesComponent. The revised
AppComponent template looks like this:
src/app/app.component.html (heroes RouterLink)
<h1>{{title}}</h1>
<nav>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
10 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
selector for the RouterLink directive that turns user clicks into router
navigations. It's another of the public directives in the RouterModule.
The browser refreshes and displays the app title and heroes link, but not
the heroes list.
Click the link. The address bar updates to /heroes and the list of
heroes appears.
Make this and future navigation links look better by adding private CSS
styles to app.component.css as listed in the final code review below.
Routing makes more sense when there are multiple views. So far there's
only the heroes view.
Add a DashboardComponent using the CLI:
The CLI generates the files for the DashboardComponent and declares
11 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
it in AppModule.
Replace the default file content in these three files as follows:
<h3>Top Heroes</h3>
<div class="grid grid-pad">
<a *ngFor="let hero of heroes" class="col-1-4">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
</a>
</div>
12 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(heroes => this.heroes =
heroes.slice(1, 5));
}
13 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
14 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
When the app starts, the browser's address bar points to the web site's
root. That doesn't match any existing route so the router doesn't navigate
anywhere. The space below the <router-outlet> is blank.
To make the app navigate to the dashboard automatically, add the
following route to the AppRoutingModule.Routes array.
src/app/app-routing.module.ts
This route redirects a URL that fully matches the empty path to the route
whose path is '/dashboard'.
After the browser refreshes, the router loads the DashboardComponent
and the browser address bar shows the /dashboard URL.
The user should be able to navigate back and forth between the
15 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard">Dashboard</a>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
After the browser refreshes you can navigate freely between the two
views by clicking the links.
16 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
When the user clicks a hero item in the HeroesComponent, the app
should navigate to the HeroDetailComponent, replacing the heroes
list view with the hero detail view. The heroes list view should no longer
show hero details as it does now.
17 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
18 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
src/app/app-routing.module.ts
The colon (:) in the path indicates that :id is a placeholder for a
specific hero id.
At this point, all application routes are in place.
src/app/app-routing.module.ts (all routes)
19 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
];
20 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
<ul class="heroes">
<li *ngFor="let hero of heroes"
[class.selected]="hero === selectedHero"
(click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span>
{{hero.name}}
</li>
</ul>
Strip the <li> back to just its *ngFor, wrap the badge and name in an
anchor element (<a>), and add a routerLink attribute to the anchor
that is the same as in the dashboard template
21 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
<ul class="heroes">
<li *ngFor="let hero of heroes">
<a routerLink="/detail/{{hero.id}}">
<span class="badge">{{hero.id}}</span>
{{hero.name}}
</a>
</li>
</ul>
22 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
It's nice to tidy up and you'll be grateful to yourself later. Here's the class
after pruning away the dead code.
src/app/heroes/heroes.component.ts (cleaned up)
ngOnInit() {
this.getHeroes();
}
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes);
}
}
23 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
Routable HeroDetailComponent
24 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
constructor(
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location
) {}
25 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
route's parameters extracted from the URL. The "id" parameter is the id
of the hero to display.
The HeroService gets hero data from the remote server and this
component will use it to get the hero-to-display.
The location is an Angular service for interacting with the browser.
You'll use it later to navigate back to the view that navigated here.
In the ngOnInit() lifecycle hook call getHero() and define it as
follows.
src/app/hero-detail/hero-detail.component.ts
ngOnInit(): void {
this.getHero();
}
getHero(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.heroService.getHero(id)
.subscribe(hero => this.hero = hero);
26 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
Add HeroService.getHero()
Open HeroService and add the following getHero() method with the
id after the getHeroes() method:
src/app/hero.service.ts (getHero)
27 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
Try it
The browser refreshes and the app is working again. You can click a
hero in the dashboard or in the heroes list and navigate to that hero's
detail view.
28 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
By clicking the browser's back button, you can go back to the hero list or
dashboard view, depending upon which sent you to the detail view.
It would be nice to have a button on the HeroDetail view that can do
that.
Add a go back button to the bottom of the component template and bind
it to the component's goBack() method.
src/app/hero-detail/hero-detail.component.html (back button)
29 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
goBack(): void {
this.location.back();
}
Refresh the browser and start clicking. Users can navigate around the
app, from the dashboard to hero details and back, from heroes list to the
mini detail to the hero details and back to the heroes again.
Here are the code files discussed on this page and your app should look
like this live example / download example.
30 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
31 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
AppComponent
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard">Dashboard</a>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
32 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
<app-messages></app-messages>
DashboardComponent
<h3>Top Heroes</h3>
<div class="grid grid-pad">
<a *ngFor="let hero of heroes" class="col-1-4"
routerLink="/detail/{{hero.id}}">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
</a>
</div>
HeroesComponent
33 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes">
<a routerLink="/detail/{{hero.id}}">
<span class="badge">{{hero.id}}</span>
{{hero.name}}
</a>
</li>
</ul>
HeroDetailComponent
<div *ngIf="hero">
<h2>{{hero.name | uppercase}} Details</h2>
<div><span>id: </span>{{hero.id}}</div>
<div>
34 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
<label>name:
<input [(ngModel)]="hero.name"
placeholder="name"/>
</label>
</div>
<button (click)="goBack()">go back</button>
</div>
Summary
35 de 36 13/04/2020 10:56 p. m.
Add in-app navigation with routing about:reader?url=https://angular.io/tutorial/toh-pt5
view.
You used router link parameters to navigate to the detail view of a user-
selected hero.
You shared the HeroService among multiple components.
36 de 36 13/04/2020 10:56 p. m.