SlideShare a Scribd company logo
2
Most read
4
Most read
10
Most read
Routing & Navigating
Pages in Angular 2
By :-
Deepak Mehra
Software Consultant
KNOLDUS SOFTWARE LLP
By :-
Deepak Mehra
Software Consultant
KNOLDUS SOFTWARE LLP
Agenda
● Why routing is necessary
● Define routes for pages
● Link to Routes
● Navigate from Code
● Route Guards
● Route Based Link Styling
● Lazy Loading
● Code Clean Up
Why routing is necessary?
● Routing helps in directing users to different
pages based on the option they choose on the
main page. Hence, based on the option they
choose, the required Angular Component will
be rendered to the user.
Define routes for pages
● Now, before you define routes for pages. Let’s see
● <base href>
● Most routing applications should add a <base> element to
the index.html as the first child in the <head> tag to tell the
router how to compose navigation URLs.
●
● Step 1 − Add the base reference tag in the index.html file.
● <html>
● <head><base href=”/”></head>
Define routes for pages
● Before we define define routes for the pages, Let’s create files for the
pages.
For example
Let’s create Inventory.component.ts and Product.component.ts and
write simple logic like below
import {Component} from '@angular/core';
@Component ({ selector: 'app-root',template: '<h1>Inventory</h1>'})
export class InventoryComponent {}
Link to Routes
● The RouterLink directives on the anchor tags give the
router control over those elements. The navigation paths
are fixed, so you can assign a string to the routerLink (a
"one-time" binding).
That is how you navigate from one page to another
<a routerLink="/product"
routerLinkActive="active">Product</a>
Adding an error route
● We can also add an error route. In Routing, one can also add an
error route. This can happen if the user goes to a page which does
not exist in the application.
Step1. Add a PageNotFound component as NotFound.component.ts.
Step2. {path: '**', component: PageNotFoundComponent}
** is for any route which does not fit the default route. They will be
directed to the PageNotFoundComponent component.
Navigate from code
● Of course, You can navigate from page to another using code.
Let’s see How do we do that.
We will need to inject router in the constructor and call navigate
function.
export class TestComponent {constructor(private router:
Router) {}
back() {
this.router.navigate(['/']);}}
Route Guards
● Sometimes we want to prevent a user from
going to a particular page or discourage them
from leaving a page. That’s what route guards
are designed for.
Route guards have two properties
1. Can Activate
2.Can Deactivate
Route Guards
2. Can Deactivate
We can use ‘Can deactivate’ to prevent a user
from leaving a page. This is often helpful when
a user is leaving the page before saving the
data.
Route Based Linking Style
● If you have a header in your website and you want to
highlight the currently active link then you can do that by
using “routerLinkActive”
Step1 . <li><a routerLink="/home"
routerLinkActive="active">Home</a></li>
Step2. Add css
#nav-bar li > a.active {
color: black;
Lazy Loading
● So far our app has only one module and that
is app module. Let’s see how we can add
multiple module.
Our application can be broken down into
smaller sections. We will add in a user
module.
Code Cleanup
● In order to clean up the code, we can expose all of the
imports inside the directory from a single index file and
then we can just import it with a single import line. This
is referred to as creating barrells.
Step 1. Create an index file.
Export * from ‘./test.component’
Step 2. In app module
Import {
} from ‘./index’
DEMO
References
● https://github.com/angular/angular-cli
● https://angular.io/docs
Thank you

More Related Content

What's hot (20)

PDF
Angular Directives
iFour Technolab Pvt. Ltd.
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PPT
Angular 8
Sunil OS
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
PDF
Angular Observables & RxJS Introduction
Rahat Khanna a.k.a mAppMechanic
 
PDF
Angular directives and pipes
Knoldus Inc.
 
PPTX
Angular Directives
Malla Reddy University
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PPTX
Angularjs PPT
Amit Baghel
 
PDF
Angular routing
Sultan Ahmed
 
PPTX
Angular
Mouad EL Fakir
 
PPTX
Angular 2.0 forms
Eyal Vardi
 
PDF
Intro to Asynchronous Javascript
Garrett Welson
 
PDF
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Edureka!
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PPTX
Angular
LearningTech
 
PDF
Angular
Lilia Sfaxi
 
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Introduction to angular with a simple but complete project
Jadson Santos
 
Angular 8
Sunil OS
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Angular Observables & RxJS Introduction
Rahat Khanna a.k.a mAppMechanic
 
Angular directives and pipes
Knoldus Inc.
 
Angular Directives
Malla Reddy University
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angularjs PPT
Amit Baghel
 
Angular routing
Sultan Ahmed
 
Angular 2.0 forms
Eyal Vardi
 
Intro to Asynchronous Javascript
Garrett Welson
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Edureka!
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
Angular
LearningTech
 
Angular
Lilia Sfaxi
 

Similar to Routing & Navigating Pages in Angular 2 (20)

PPTX
Angular2 routing
TejinderMakkar
 
DOCX
Angular2RoutingSetupByShubham
Shubham Verma
 
DOCX
Understand routing in angular 2
codeandyou forums
 
PDF
8 things you didn't know about the Angular Router, you won't believe #6!
Laurent Duveau
 
PDF
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
Katy Slemon
 
PPTX
UQ21CA642BA1-Unit-3-WAF-Class18-Introduction to Angular Routing.pptx
TamalDey28
 
PPTX
Angular 2
alinabiliashevych
 
PDF
Angular2 workshop
Nir Kaufman
 
PPTX
Neoito — Routing and navigation in Angular
Neoito
 
PDF
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
Edureka!
 
PDF
Routing in NEXTJS.pdf
AnishaDahal5
 
PPTX
Routing And Navigation
Eyal Vardi
 
PPTX
ngNewRouter
phidong
 
PPTX
Unit 2 - Data Binding.pptx
Malla Reddy University
 
PDF
Angular_BootStrap.pdf
raja344116
 
PPTX
Angular 2.0 Routing and Navigation
Eyal Vardi
 
PDF
Angular JS2 Training Session #2
Paras Mendiratta
 
PDF
Mastering angular - Dot Net Tricks
Gaurav Singh
 
PDF
Angular Routing - Angular Hack Day Melbourne 2019
Jernej Kavka (JK)
 
PDF
Understanding router state in angular 7 passing data through angular router s...
Katy Slemon
 
Angular2 routing
TejinderMakkar
 
Angular2RoutingSetupByShubham
Shubham Verma
 
Understand routing in angular 2
codeandyou forums
 
8 things you didn't know about the Angular Router, you won't believe #6!
Laurent Duveau
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
Katy Slemon
 
UQ21CA642BA1-Unit-3-WAF-Class18-Introduction to Angular Routing.pptx
TamalDey28
 
Angular2 workshop
Nir Kaufman
 
Neoito — Routing and navigation in Angular
Neoito
 
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
Edureka!
 
Routing in NEXTJS.pdf
AnishaDahal5
 
Routing And Navigation
Eyal Vardi
 
ngNewRouter
phidong
 
Unit 2 - Data Binding.pptx
Malla Reddy University
 
Angular_BootStrap.pdf
raja344116
 
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Angular JS2 Training Session #2
Paras Mendiratta
 
Mastering angular - Dot Net Tricks
Gaurav Singh
 
Angular Routing - Angular Hack Day Melbourne 2019
Jernej Kavka (JK)
 
Understanding router state in angular 7 passing data through angular router s...
Katy Slemon
 
Ad

More from Knoldus Inc. (20)

PPTX
Angular Hydration Presentation (FrontEnd)
Knoldus Inc.
 
PPTX
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Knoldus Inc.
 
PPTX
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
PPTX
Kanban Metrics Presentation (Project Management)
Knoldus Inc.
 
PPTX
Java 17 features and implementation.pptx
Knoldus Inc.
 
PPTX
Chaos Mesh Introducing Chaos in Kubernetes
Knoldus Inc.
 
PPTX
GraalVM - A Step Ahead of JVM Presentation
Knoldus Inc.
 
PPTX
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
PPTX
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
PPTX
DAPR - Distributed Application Runtime Presentation
Knoldus Inc.
 
PPTX
Introduction to Azure Virtual WAN Presentation
Knoldus Inc.
 
PPTX
Introduction to Argo Rollouts Presentation
Knoldus Inc.
 
PPTX
Intro to Azure Container App Presentation
Knoldus Inc.
 
PPTX
Insights Unveiled Test Reporting and Observability Excellence
Knoldus Inc.
 
PPTX
Introduction to Splunk Presentation (DevOps)
Knoldus Inc.
 
PPTX
Code Camp - Data Profiling and Quality Analysis Framework
Knoldus Inc.
 
PPTX
AWS: Messaging Services in AWS Presentation
Knoldus Inc.
 
PPTX
Amazon Cognito: A Primer on Authentication and Authorization
Knoldus Inc.
 
PPTX
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Knoldus Inc.
 
PPTX
Managing State & HTTP Requests In Ionic.
Knoldus Inc.
 
Angular Hydration Presentation (FrontEnd)
Knoldus Inc.
 
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Knoldus Inc.
 
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
Kanban Metrics Presentation (Project Management)
Knoldus Inc.
 
Java 17 features and implementation.pptx
Knoldus Inc.
 
Chaos Mesh Introducing Chaos in Kubernetes
Knoldus Inc.
 
GraalVM - A Step Ahead of JVM Presentation
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
DAPR - Distributed Application Runtime Presentation
Knoldus Inc.
 
Introduction to Azure Virtual WAN Presentation
Knoldus Inc.
 
Introduction to Argo Rollouts Presentation
Knoldus Inc.
 
Intro to Azure Container App Presentation
Knoldus Inc.
 
Insights Unveiled Test Reporting and Observability Excellence
Knoldus Inc.
 
Introduction to Splunk Presentation (DevOps)
Knoldus Inc.
 
Code Camp - Data Profiling and Quality Analysis Framework
Knoldus Inc.
 
AWS: Messaging Services in AWS Presentation
Knoldus Inc.
 
Amazon Cognito: A Primer on Authentication and Authorization
Knoldus Inc.
 
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Knoldus Inc.
 
Managing State & HTTP Requests In Ionic.
Knoldus Inc.
 
Ad

Recently uploaded (20)

PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Executive Business Intelligence Dashboards
vandeslie24
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Human Resources Information System (HRIS)
Amity University, Patna
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 

Routing & Navigating Pages in Angular 2

  • 1. Routing & Navigating Pages in Angular 2 By :- Deepak Mehra Software Consultant KNOLDUS SOFTWARE LLP By :- Deepak Mehra Software Consultant KNOLDUS SOFTWARE LLP
  • 2. Agenda ● Why routing is necessary ● Define routes for pages ● Link to Routes ● Navigate from Code ● Route Guards ● Route Based Link Styling ● Lazy Loading ● Code Clean Up
  • 3. Why routing is necessary? ● Routing helps in directing users to different pages based on the option they choose on the main page. Hence, based on the option they choose, the required Angular Component will be rendered to the user.
  • 4. Define routes for pages ● Now, before you define routes for pages. Let’s see ● <base href> ● Most routing applications should add a <base> element to the index.html as the first child in the <head> tag to tell the router how to compose navigation URLs. ● ● Step 1 − Add the base reference tag in the index.html file. ● <html> ● <head><base href=”/”></head>
  • 5. Define routes for pages ● Before we define define routes for the pages, Let’s create files for the pages. For example Let’s create Inventory.component.ts and Product.component.ts and write simple logic like below import {Component} from '@angular/core'; @Component ({ selector: 'app-root',template: '<h1>Inventory</h1>'}) export class InventoryComponent {}
  • 6. Link to Routes ● The RouterLink directives on the anchor tags give the router control over those elements. The navigation paths are fixed, so you can assign a string to the routerLink (a "one-time" binding). That is how you navigate from one page to another <a routerLink="/product" routerLinkActive="active">Product</a>
  • 7. Adding an error route ● We can also add an error route. In Routing, one can also add an error route. This can happen if the user goes to a page which does not exist in the application. Step1. Add a PageNotFound component as NotFound.component.ts. Step2. {path: '**', component: PageNotFoundComponent} ** is for any route which does not fit the default route. They will be directed to the PageNotFoundComponent component.
  • 8. Navigate from code ● Of course, You can navigate from page to another using code. Let’s see How do we do that. We will need to inject router in the constructor and call navigate function. export class TestComponent {constructor(private router: Router) {} back() { this.router.navigate(['/']);}}
  • 9. Route Guards ● Sometimes we want to prevent a user from going to a particular page or discourage them from leaving a page. That’s what route guards are designed for. Route guards have two properties 1. Can Activate 2.Can Deactivate
  • 10. Route Guards 2. Can Deactivate We can use ‘Can deactivate’ to prevent a user from leaving a page. This is often helpful when a user is leaving the page before saving the data.
  • 11. Route Based Linking Style ● If you have a header in your website and you want to highlight the currently active link then you can do that by using “routerLinkActive” Step1 . <li><a routerLink="/home" routerLinkActive="active">Home</a></li> Step2. Add css #nav-bar li > a.active { color: black;
  • 12. Lazy Loading ● So far our app has only one module and that is app module. Let’s see how we can add multiple module. Our application can be broken down into smaller sections. We will add in a user module.
  • 13. Code Cleanup ● In order to clean up the code, we can expose all of the imports inside the directory from a single index file and then we can just import it with a single import line. This is referred to as creating barrells. Step 1. Create an index file. Export * from ‘./test.component’ Step 2. In app module Import { } from ‘./index’
  • 14. DEMO