Angular - Add Navigation With Routing
Angular - Add Navigation With Routing
• When users click a deep link in an email, open the detail view for a
particular hero
For the sample application that this page describes, see the
When you're done, users can navigate the application like this:
Add the
In Angular, the best practice is to load and con�gure the router in a
PARAMETER DETAILS
{ } '@angular∕core';
{ } '@angular∕common';
@NgModule({
imports: [
],
declarations: []
})
{ }
src/app/app-routing.module.ts (updated)
{ } '@angular∕core';
{ , }
'@angular∕router';
{ } '.∕heroes
∕heroes.component';
routes: = [
{ path: 'heroes', component: }
];
@NgModule({
imports: [ .forRoot(routes)],
exports: [ ]
})
{ }
Routes so the application can have routing capability. The next import,
detail.
Routes
The next part of the �le is where you con�gure your routes. Routes tell
the Router which view to display when a user clicks a link or pastes a
src/app/app-routing.module.ts
routes: = [
{ path: 'heroes', component: }
];
PROPERTIES DETAILS
This tells the router to match that URL to path: 'heroes' and display
localhost:4200∕heroes .
The @NgModule metadata initializes the router and starts it listening for
imports array and con�gures it with the routes in one step by calling
RouterModule.forRoot() :
src/app/app-routing.module.ts
imports: [ .forRoot(routes) ],
exports: [ ]
Add
Open the AppComponent template and replace the <app-heroes>
{{title}}
navigates to it.
NgModule .
Try it
If you're not still serving your application, run ng serve to see your
The browser should refresh and display the application 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 overview/detail view.
Remove ∕heroes from the URL in the browser address bar. The
browser should refresh and display the application title but not the list of
heroes.
Add a <nav> element and, within that, an anchor element that, when
{{title}}
routerLink="∕heroes" Heroes
selector for the RouterLink directive that turns user clicks into router
The browser refreshes and displays the application title and heroes link,
Click the link. The address bar updates to ∕heroes and the list of
heroes appears.