Lesson 11 Routing
Lesson 11 Routing
Lesson 11 Routing
Lesson 11—Routing
Angular Routers:
The URL bar provides an edge to web applications over native applications.
• Routers permit you to reference states, bookmark them, and share them.
• In a good web application, an application state transition results in a URL change, and a
modification in the URL alters the transition state. A URL is a serialized router state.
Routing
Topic 2—Single Page Application (SPA)
Single Page Application (SPA)
There is no need to refresh the entire page. You can just load the part of the
No page
refresh
page that needs to be changed. Angular permits you to pre-load and cache all
pages, so you don’t require extra requests to download them.
Improved
user SPAs act as native applications, which are fast and responsive.
experience
Ability to With SPAs, all pages are already loaded, so they work even if the internet
work offline
connection is lost.
Cons of Single Page Application (SPA)
Complex to build You are required to write JavaScript, handle shared state
between pages, manage permissions, and more.
Slow initial load SPA is slow in the initial load as it needs to download more
resources when you open it.
• Angular Router is an optional service that displays a distinct component view for a given URL.
• It is not a part of Angular core. It is in its own library package, @angular/router. You can import per
your requirement as any other Angular package.
NgModule is a way to organize dependencies for the compiler and dependency injection (DI).
1 Dependency Injection
>_ training@localhost:~
@NgModule({
imports: [
RouterModule.forChild([…]),
CommonModule
],
providers: [
HomeService
],
declarations: [
HomeComponent
]
})
Export default class HomeModule{}
Routing
Topic 4—Router Basic
Router Basic
This is a mapping file where you can map components to their target URL.
>_ training@localhost:~
Const routes = [
{path: ‘’, redirectTo: ‘home’}, //Index router
{path: ‘home’, component: HomeComponent},
{path: ‘guards’, component: GuardsComponent},
{path: ‘resolver’, component: ResolverComponent},
{path: ‘preview’, component: PreviewComponent},
{path: ‘**’, component: E404Component //Fallback router
];
Router Basic—Register Root Router
>_ training@localhost:~
routerLink Directive:
>_ training@localhost:~
<a [routerLink]=“[‘/home’]”>Home</a>
<a [routerLink]=“[‘/guards’]”>Guards</a>
<a [routerLink]=“[‘/resolver’]”>Resolver</a>
<a [routerLink]=“[‘/preview’]”>Preview</a>
Router Basic—Rendering the Page
Acts as a placeholder that Angular dynamically fills based on the current router state
>_ training@localhost:~
<router-outlet></router-outlet>
Demo—Basic Routing
DEMO
Routing
Topic 5—Parameter Routing
Parameter Routing
• For example, you have a news website with many articles. Every article may have an ID, and there is
/articles/3
/articles/4
Parameter Routing
This is the way to indicate the route path by adding a parameter to router configuration:
>_ training@localhost:~
DEMO
Routing
Topic 6—Child Routes
Child Routes
Sometimes, when routes are accessible and viewed only within other routes, it is more relevant
to create them as child routes.
Example :
• There is a tabbed navigations section in the product details page that presents the
product overview by default.
• When the user clicks on the "Technical Specs" tab, the section shows the specs instead.
Declaration of Child Route
Example :
• In case the user clicks on the link of ID 3, show the user details page with the “overview”:
localhost:3000/user-details/3/overview
• Here, overview and skills are child routes of user-details/:id. They are reachable only
within user details.
Child Routing Page
Example:
• In this page, both child one and two are child routes of component two.
Demo—Child Routing
DEMO
Routing
Topic 7—Router Lifecycle Hooks
Router Lifecycle Hooks
• Each one is accessible from a different lifecycle hook which, just like
components, can be handled by implementing a specific interface in the
component subject of the routing action.
The CanActivate Hook
• The CanActivate hook, presented as a decorator annotating the component, is checked by the Router
right before it is instantiated.
• Its “set up” should be configured with a function that returns a Boolean value (or a
Promise-typed Boolean value) indicating whether the component should be finally
activated or not.
• The OnActivate hook allows you to perform custom actions once the route navigation to the
• These custom actions can even encompass asynchronous operations, in which case, you need
• The route will only change once the Promise has been resolved.
The CanDeactivate and OnDeactivate Hooks
• The logic you use to determine whether the component you navigate to can be activated can also be
used when a user navigates from current component to another one elsewhere in your application.
• In case of the CanActivate hook, you must return a Boolean or a Promise resolving to a Boolean to
• When the CanDeactivate hook returns, or is resolved to true, the OnDeactivate hook is executed just
DEMO
Key Takeaways
Angular uses a lifecycle hook to carry out actions when changing routes.
Quiz
QUIZ The ___________ directive substitutes the normal href property and makes it easier to
1 work with route links in Angular.
a. RouterLink
b. RouterRend
c. RouterLike
d. RouterLayer
QUIZ The ___________ directive substitutes the normal href property and makes it easier to
1 work with route links in Angular.
a. RouterLink
b. RouterRend
c. RouterLike
d. RouterLayer
d. URL Unresolver
d. URL Unresolver