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

PPT
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
PPTX
Secure Coding 101 - OWASP University of Ottawa Workshop
Paul Ionescu
 
PPTX
Angular 2.0 forms
Eyal Vardi
 
PPTX
Secure coding practices
Mohammed Danish Amber
 
PDF
Conditional Statements | If-then Statements
sheisirenebkm
 
PPTX
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
PPTX
Angular modules in depth
Christoffer Noring
 
PDF
IB Grade 1 Math Workbook Sample.pdf
balajiv2020
 
Angular Introduction By Surekha Gadkari
Surekha Gadkari
 
Secure Coding 101 - OWASP University of Ottawa Workshop
Paul Ionescu
 
Angular 2.0 forms
Eyal Vardi
 
Secure coding practices
Mohammed Danish Amber
 
Conditional Statements | If-then Statements
sheisirenebkm
 
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Angular modules in depth
Christoffer Noring
 
IB Grade 1 Math Workbook Sample.pdf
balajiv2020
 

What's hot (20)

PDF
Angular Advanced Routing
Laurent Duveau
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Angular data binding
Sultan Ahmed
 
PDF
Angular routing
Sultan Ahmed
 
PDF
Angular Pipes Workshop
Nir Kaufman
 
PPTX
Angular tutorial
Rohit Gupta
 
PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PPT
Angular 8
Sunil OS
 
PPTX
Angular 9
Raja Vishnu
 
PPTX
Angular Directives
Malla Reddy University
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PDF
Angular Directives
iFour Technolab Pvt. Ltd.
 
PPTX
Angular introduction students
Christian John Felix
 
PPTX
Angular overview
Thanvilahari
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPTX
Angularjs PPT
Amit Baghel
 
PDF
Angular Dependency Injection
Nir Kaufman
 
PPTX
Typescript in 30mins
Udaya Kumar
 
PDF
The New JavaScript: ES6
Rob Eisenberg
 
Angular Advanced Routing
Laurent Duveau
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular data binding
Sultan Ahmed
 
Angular routing
Sultan Ahmed
 
Angular Pipes Workshop
Nir Kaufman
 
Angular tutorial
Rohit Gupta
 
Introduction to angular with a simple but complete project
Jadson Santos
 
Angular 8
Sunil OS
 
Angular 9
Raja Vishnu
 
Angular Directives
Malla Reddy University
 
Angular Data Binding
Jennifer Estrada
 
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular introduction students
Christian John Felix
 
Angular overview
Thanvilahari
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
jQuery for beginners
Arulmurugan Rajaraman
 
Angularjs PPT
Amit Baghel
 
Angular Dependency Injection
Nir Kaufman
 
Typescript in 30mins
Udaya Kumar
 
The New JavaScript: ES6
Rob Eisenberg
 
Ad

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

PPTX
Angular2 routing
TejinderMakkar
 
PPTX
Angular 2 at solutions.hamburg
Baqend
 
PDF
Building an Angular 2 App
Felix Gessert
 
DOCX
Understand routing in angular 2
codeandyou forums
 
PDF
Web components - An Introduction
cherukumilli2
 
PPTX
ngNewRouter
phidong
 
PDF
Routing in NEXTJS.pdf
AnishaDahal5
 
PDF
Learn more about React Router & it's properties
React Masters
 
PPTX
Neoito — Routing and navigation in Angular
Neoito
 
DOCX
Angular2RoutingSetupByShubham
Shubham Verma
 
PPTX
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
PPTX
UQ21CA642BA1-Unit-3-WAF-Class18-Introduction to Angular Routing.pptx
TamalDey28
 
PDF
Angular.js for beginners
Basia Madej
 
PPTX
How to implement multiple layouts using React router V4.pptx
BOSC Tech Labs
 
PPTX
Unit 2 - Data Binding.pptx
Malla Reddy University
 
PPTX
Angular js 2.0 beta
Nagaraju Sangam
 
PDF
02 - Angular Structural Elements - 1.pdf
BrunoOliveira631137
 
PPTX
Foster - Getting started with Angular
MukundSonaiya1
 
PPTX
AngularJS Fundamentals + WebAPI
Eric Wise
 
PPTX
React JS CONCEPT AND DETAILED EXPLANATION
harshavardhanjuttika
 
Angular2 routing
TejinderMakkar
 
Angular 2 at solutions.hamburg
Baqend
 
Building an Angular 2 App
Felix Gessert
 
Understand routing in angular 2
codeandyou forums
 
Web components - An Introduction
cherukumilli2
 
ngNewRouter
phidong
 
Routing in NEXTJS.pdf
AnishaDahal5
 
Learn more about React Router & it's properties
React Masters
 
Neoito — Routing and navigation in Angular
Neoito
 
Angular2RoutingSetupByShubham
Shubham Verma
 
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
UQ21CA642BA1-Unit-3-WAF-Class18-Introduction to Angular Routing.pptx
TamalDey28
 
Angular.js for beginners
Basia Madej
 
How to implement multiple layouts using React router V4.pptx
BOSC Tech Labs
 
Unit 2 - Data Binding.pptx
Malla Reddy University
 
Angular js 2.0 beta
Nagaraju Sangam
 
02 - Angular Structural Elements - 1.pdf
BrunoOliveira631137
 
Foster - Getting started with Angular
MukundSonaiya1
 
AngularJS Fundamentals + WebAPI
Eric Wise
 
React JS CONCEPT AND DETAILED EXPLANATION
harshavardhanjuttika
 
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.
 

Recently uploaded (20)

PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Activate_Methodology_Summary presentatio
annapureddyn
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 

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