SlideShare a Scribd company logo
1 of 7
Understand Routing in
Angular 2
Angular2 still indevelopmentphase. Routingisimportantpartof any SPA (Single Page Application).
Angular2 provide aneasy way to implementrouting.
Angular 2 supports both location strategies using its classes HashLocationStrategy and
PathLocationStrategy. HashLocationStrategy handle by adding the hash sign to the URL and
pushState() method.
Angular 2 identified by the tags <router-link></router-link>
Angular 2 use HTML5 APIs to programmatically support the Back and Forward buttons and
pushState() check the state of URL. User’s navigations history maintain on client side. If view
already loaded into memory then request not goes to the server.
Today I am giving example of Angular 2 routing using HashLocationStrategy.
First we need to understand the main component of Routing in Angular 2.
 router-link – router-link directive is use to declare link into view . Its can contains
optional parameters also.
Example :
<a [router-link]="['/AboutUs']">About Us</a>
 router-outlet – Its work as a placeholder for views to render then component. Means
template and templateUrl will be render on that location where you will use router-
outlet directive.
Example :
<router-outlet></router-outlet>
 @RouteConfig – We map URLs to components in this section which used inside the
<router-link></router-link>.
Example :
@RouteConfig([
{path: '/', component: HomeComponent, as: 'Home'},
{path: '/aboutus', component: AboutUsComponent, as: 'AboutUs' }
{path: '/contactus', component: ContactUsComponent, as: 'ContactUs' }
])
 RouteParams – Parameter to a component which rendered by the router.
Steps for use routing in Angular 2
1. Add Angular 2 routing library into your html file https://code.angularjs.org/2.0.0-
alpha.45/router.dev.js
<script src="https://code.angularjs.org/2.0.0-alpha.45/router.dev.js"></script>
2. Import all routing directive and classes from 'angular2/router'
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS,
LocationStrategy, HashLocationStrategy} from 'angular2/router';
3. Map URLs in @RouteConfig
@RouteConfig([
{path: '/', component: HomeComponent,as:'Home'},
{path: '/aboutus',component:AboutUsComponent,as:'AboutUs' }
{path: '/contactus',component: ContactUsComponent,as:'ContactUs' }
])
4. In main root component use router-outlet where you want render components
<router-outlet></router-outlet>
5. Define router-link for which component you want to show
<router-link></router-link>.
Plnkr - http://plnkr.co/edit/2de7CWiUrRMpl3wJgwbT?p=preview
Routing Example in Angular 2
index.html
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> UNDERSTAND ROUTING INANGULAR2</TITLE>
<SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/TOOLS/SYSTEM.JS"></SCRIPT>
<SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/TOOLS/TYPESCRIPT.JS"></SCRIPT>
<SCRIPT SRC="CONFIG.JS"></SCRIPT>
<SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/2.0.0-ALPHA.45/ANGULAR2.DEV.JS"></SCRIPT>
<SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/2.0.0-ALPHA.45/ROUTER.DEV.JS"></SCRIPT>
<SCRIPT>
SYSTEM.IMPORT('./APP.TS');
</SCRIPT>
</HEAD>
<BODY>
<MAINROOT></MAINROOT>
</BODY>
</HTML>
app.ts
IMPORT {COMPONENT,BOOTSTRAP,PROVIDE} FROM 'ANGULAR2/ANGULAR2';
IMPORT {ROUTECONFIG, ROUTER_DIRECTIVES, ROUTER_PROVIDERS,
LOCATIONSTRATEGY,HASHLOCATIONSTRATEGY}FROM 'ANGULAR2/ROUTER';
//HOME COMPONENT
@COMPONENT({
SELECTOR:'HOME',
TEMPLATE:'<H2>WELCOME TO <A HREF="HTTP://WWW.CODEANDYOU.COM"TARGET="_BLANK">
WWW.CODEANDYOU.COM</A></H2>',
STYLES:['.HOME {BACKGROUND:RED}'],
})
EXPORT CLASS HOMECOMPONENT {}
//ABOUT US COMPONENT
@COMPONENT({
SELECTOR:'ABOUTUS',
TEMPLATEURL:'ABOUTUS.HTML'
})
EXPORT CLASS ABOUTUSCOMPONENT {}
//CONTACT US COMPONENT
@COMPONENT({
SELECTOR:'CONTACTUS',
TEMPLATE:'<H2>CONTACT US</H2>'
})
EXPORT CLASS CONTACTUSCOMPONENT {}
//ROOT COMPONENT
@COMPONENT({
SELECTOR:'MAINROOT',
TEMPLATE:`<DIV CLASS="GRAYCOLOR"><A [ROUTER-LINK]="['/HOME']">HOME</A>
<A [ROUTER-LINK]="['/ABOUTUS']">ABOUTUS</A>
<A [ROUTER-LINK]="['/CONTACTUS']">CONTACT US</A>
</DIV><HR>
<ROUTER-OUTLET></ROUTER-OUTLET>
`,
DIRECTIVES:[ ROUTER_DIRECTIVES]
})
@ROUTECONFIG([
{PATH:'/', COMPONENT: HOMECOMPONENT,AS:'HOME'},
{PATH:'/ABOUTUS', COMPONENT:ABOUTUSCOMPONENT,AS:'ABOUTUS' }
{PATH:'/CONTACTUS',COMPONENT:CONTACTUSCOMPONENT,AS:'CONTACTUS' }
])
CLASS ROOTCOMPONENT{
}
BOOTSTRAP(ROOTCOMPONENT,[ROUTER_PROVIDERS,PROVIDE(LOCATIONSTRATEGY,{USECLASS:
HASHLOCATIONSTRATEGY})]);
config.js
SYSTEM.CONFIG({
//USE TYPESCRIPT FOR COMPILATION
TRANSPILER:'TYPESCRIPT',
//TYPESCRIPT COMPILER OPTIONS
TYPESCRIPTOPTIONS:{
EMITDECORATORMETADATA:TRUE
}
});
aboutUs.html
<H2>ABOUT US</H2>
Thanks
www.codeandyou.com
http://www.codeandyou.com/2015/11/understand-
routing-in-angular-2.html
Keywords - Understand Routing in Angular 2, How to use Routing in Angular 2,
routing in Angular 2, Angular 2 Routing

More Related Content

What's hot

The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2Maurice De Beijer [MVP]
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in AngularAlexe Bogdan
 
Angular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamAngular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamShubham Verma
 
SocketIOSetupWithAngularJSAppByShubham
SocketIOSetupWithAngularJSAppByShubhamSocketIOSetupWithAngularJSAppByShubham
SocketIOSetupWithAngularJSAppByShubhamShubham Verma
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency InjectionNir Kaufman
 
AngularJS - dependency injection
AngularJS - dependency injectionAngularJS - dependency injection
AngularJS - dependency injectionAlexe Bogdan
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And NavigationEyal Vardi
 
Angular 1.x in action now
Angular 1.x in action nowAngular 1.x in action now
Angular 1.x in action nowGDG Odessa
 
هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!Web Standards School
 
The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2Maurice De Beijer [MVP]
 
Angular js 2
Angular js 2Angular js 2
Angular js 2Ran Wahle
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIVisual Engineering
 
Peggy angular 2 in meteor
Peggy   angular 2 in meteorPeggy   angular 2 in meteor
Peggy angular 2 in meteorLearningTech
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
Express: A Jump-Start
Express: A Jump-StartExpress: A Jump-Start
Express: A Jump-StartNaveen Pete
 
Beyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreBeyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreAri Lerner
 
Angular components
Angular componentsAngular components
Angular componentsSultan Ahmed
 

What's hot (20)

The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in Angular
 
Angular2RoutingSetupByShubham
Angular2RoutingSetupByShubhamAngular2RoutingSetupByShubham
Angular2RoutingSetupByShubham
 
SocketIOSetupWithAngularJSAppByShubham
SocketIOSetupWithAngularJSAppByShubhamSocketIOSetupWithAngularJSAppByShubham
SocketIOSetupWithAngularJSAppByShubham
 
Angular routing
Angular routingAngular routing
Angular routing
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
AngularJS - dependency injection
AngularJS - dependency injectionAngularJS - dependency injection
AngularJS - dependency injection
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular 1.x in action now
Angular 1.x in action nowAngular 1.x in action now
Angular 1.x in action now
 
هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!
 
The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
 
Elefrant [ng-Poznan]
Elefrant [ng-Poznan]Elefrant [ng-Poznan]
Elefrant [ng-Poznan]
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
 
Peggy angular 2 in meteor
Peggy   angular 2 in meteorPeggy   angular 2 in meteor
Peggy angular 2 in meteor
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Express: A Jump-Start
Express: A Jump-StartExpress: A Jump-Start
Express: A Jump-Start
 
Beyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreBeyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and more
 
Angular components
Angular componentsAngular components
Angular components
 

Similar to Understand routing in angular 2

Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Frost
 
Introduction to Angular js 2.0
Introduction to Angular js 2.0Introduction to Angular js 2.0
Introduction to Angular js 2.0Nagaraju Sangam
 
Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Knoldus Inc.
 
Angular 2: Migration - SSD 2016 London
Angular 2: Migration - SSD 2016 LondonAngular 2: Migration - SSD 2016 London
Angular 2: Migration - SSD 2016 LondonManfred Steyer
 
React Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for DevelopersReact Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for DevelopersInexture Solutions
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6William Marques
 
Angular 2 at solutions.hamburg
Angular 2 at solutions.hamburgAngular 2 at solutions.hamburg
Angular 2 at solutions.hamburgBaqend
 
How to implement multiple layouts using React router V4.pptx
How to implement multiple layouts using React router V4.pptxHow to implement multiple layouts using React router V4.pptx
How to implement multiple layouts using React router V4.pptxBOSC Tech Labs
 
Building an Angular 2 App
Building an Angular 2 AppBuilding an Angular 2 App
Building an Angular 2 AppFelix Gessert
 
Routing in NEXTJS.pdf
Routing in NEXTJS.pdfRouting in NEXTJS.pdf
Routing in NEXTJS.pdfAnishaDahal5
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JSBipin
 
Voorhoede - Front-end architecture
Voorhoede - Front-end architectureVoorhoede - Front-end architecture
Voorhoede - Front-end architectureJasper Moelker
 
はじめてのAngular2
はじめてのAngular2はじめてのAngular2
はじめてのAngular2Kenichi Kanai
 
8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!Laurent Duveau
 

Similar to Understand routing in angular 2 (20)

Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in Angular
 
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
Building an angular application -1 ( API: Golang, Database: Postgres) v1.0
 
Introduction to Angular js 2.0
Introduction to Angular js 2.0Introduction to Angular js 2.0
Introduction to Angular js 2.0
 
Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2
 
Angular 2: Migration - SSD 2016 London
Angular 2: Migration - SSD 2016 LondonAngular 2: Migration - SSD 2016 London
Angular 2: Migration - SSD 2016 London
 
React Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for DevelopersReact Router Dom Integration Tutorial for Developers
React Router Dom Integration Tutorial for Developers
 
Angular2 routing
Angular2 routingAngular2 routing
Angular2 routing
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
Angular 2
Angular 2Angular 2
Angular 2
 
Angular 2 at solutions.hamburg
Angular 2 at solutions.hamburgAngular 2 at solutions.hamburg
Angular 2 at solutions.hamburg
 
Angular js 2.0
Angular js 2.0Angular js 2.0
Angular js 2.0
 
Angular js 2.0 beta
Angular js 2.0 betaAngular js 2.0 beta
Angular js 2.0 beta
 
How to implement multiple layouts using React router V4.pptx
How to implement multiple layouts using React router V4.pptxHow to implement multiple layouts using React router V4.pptx
How to implement multiple layouts using React router V4.pptx
 
Building an Angular 2 App
Building an Angular 2 AppBuilding an Angular 2 App
Building an Angular 2 App
 
Routing in NEXTJS.pdf
Routing in NEXTJS.pdfRouting in NEXTJS.pdf
Routing in NEXTJS.pdf
 
Angular2 + rxjs
Angular2 + rxjsAngular2 + rxjs
Angular2 + rxjs
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 
Voorhoede - Front-end architecture
Voorhoede - Front-end architectureVoorhoede - Front-end architecture
Voorhoede - Front-end architecture
 
はじめてのAngular2
はじめてのAngular2はじめてのAngular2
はじめてのAngular2
 
8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!8 things you didn't know about the Angular Router, you won't believe #6!
8 things you didn't know about the Angular Router, you won't believe #6!
 

More from codeandyou forums

How to validate server certificate
How to validate server certificateHow to validate server certificate
How to validate server certificatecodeandyou forums
 
How to call $scope function from console
How to call $scope function from consoleHow to call $scope function from console
How to call $scope function from consolecodeandyou forums
 
Understand components in Angular 2
Understand components in Angular 2Understand components in Angular 2
Understand components in Angular 2codeandyou forums
 
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?codeandyou forums
 
How to install ssl certificate from .pem
How to install ssl certificate from .pemHow to install ssl certificate from .pem
How to install ssl certificate from .pemcodeandyou forums
 
Protractor end-to-end testing framework for angular js
Protractor   end-to-end testing framework for angular jsProtractor   end-to-end testing framework for angular js
Protractor end-to-end testing framework for angular jscodeandyou forums
 
How routing works in angular js
How routing works in angular jsHow routing works in angular js
How routing works in angular jscodeandyou forums
 
How to use proxy server in .net application
How to use proxy server in .net applicationHow to use proxy server in .net application
How to use proxy server in .net applicationcodeandyou forums
 
How to catch query string in angular js
How to catch query string in angular jsHow to catch query string in angular js
How to catch query string in angular jscodeandyou forums
 
Different way to share data between controllers in angular js
Different way to share data between controllers in angular jsDifferent way to share data between controllers in angular js
Different way to share data between controllers in angular jscodeandyou forums
 
Angular.js interview questions
Angular.js interview questionsAngular.js interview questions
Angular.js interview questionscodeandyou forums
 
How to set up a proxy server on windows
How to set up a proxy server on windows How to set up a proxy server on windows
How to set up a proxy server on windows codeandyou forums
 
What is $root scope in angularjs
What is $root scope in angularjsWhat is $root scope in angularjs
What is $root scope in angularjscodeandyou forums
 
How to save log4net into database
How to save log4net into databaseHow to save log4net into database
How to save log4net into databasecodeandyou forums
 

More from codeandyou forums (17)

How to validate server certificate
How to validate server certificateHow to validate server certificate
How to validate server certificate
 
How to call $scope function from console
How to call $scope function from consoleHow to call $scope function from console
How to call $scope function from console
 
Understand components in Angular 2
Understand components in Angular 2Understand components in Angular 2
Understand components in Angular 2
 
How to setup ionic 2
How to setup ionic 2How to setup ionic 2
How to setup ionic 2
 
MongoDB 3.2.0 Released
MongoDB 3.2.0 ReleasedMongoDB 3.2.0 Released
MongoDB 3.2.0 Released
 
Welcome to ionic 2
Welcome to ionic 2Welcome to ionic 2
Welcome to ionic 2
 
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
 
How to install ssl certificate from .pem
How to install ssl certificate from .pemHow to install ssl certificate from .pem
How to install ssl certificate from .pem
 
Protractor end-to-end testing framework for angular js
Protractor   end-to-end testing framework for angular jsProtractor   end-to-end testing framework for angular js
Protractor end-to-end testing framework for angular js
 
How routing works in angular js
How routing works in angular jsHow routing works in angular js
How routing works in angular js
 
How to use proxy server in .net application
How to use proxy server in .net applicationHow to use proxy server in .net application
How to use proxy server in .net application
 
How to catch query string in angular js
How to catch query string in angular jsHow to catch query string in angular js
How to catch query string in angular js
 
Different way to share data between controllers in angular js
Different way to share data between controllers in angular jsDifferent way to share data between controllers in angular js
Different way to share data between controllers in angular js
 
Angular.js interview questions
Angular.js interview questionsAngular.js interview questions
Angular.js interview questions
 
How to set up a proxy server on windows
How to set up a proxy server on windows How to set up a proxy server on windows
How to set up a proxy server on windows
 
What is $root scope in angularjs
What is $root scope in angularjsWhat is $root scope in angularjs
What is $root scope in angularjs
 
How to save log4net into database
How to save log4net into databaseHow to save log4net into database
How to save log4net into database
 

Recently uploaded

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 

Recently uploaded (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 

Understand routing in angular 2

  • 2. Angular2 still indevelopmentphase. Routingisimportantpartof any SPA (Single Page Application). Angular2 provide aneasy way to implementrouting. Angular 2 supports both location strategies using its classes HashLocationStrategy and PathLocationStrategy. HashLocationStrategy handle by adding the hash sign to the URL and pushState() method. Angular 2 identified by the tags <router-link></router-link> Angular 2 use HTML5 APIs to programmatically support the Back and Forward buttons and pushState() check the state of URL. User’s navigations history maintain on client side. If view already loaded into memory then request not goes to the server. Today I am giving example of Angular 2 routing using HashLocationStrategy. First we need to understand the main component of Routing in Angular 2.  router-link – router-link directive is use to declare link into view . Its can contains optional parameters also. Example : <a [router-link]="['/AboutUs']">About Us</a>  router-outlet – Its work as a placeholder for views to render then component. Means template and templateUrl will be render on that location where you will use router- outlet directive. Example : <router-outlet></router-outlet>  @RouteConfig – We map URLs to components in this section which used inside the <router-link></router-link>. Example : @RouteConfig([ {path: '/', component: HomeComponent, as: 'Home'}, {path: '/aboutus', component: AboutUsComponent, as: 'AboutUs' }
  • 3. {path: '/contactus', component: ContactUsComponent, as: 'ContactUs' } ])  RouteParams – Parameter to a component which rendered by the router. Steps for use routing in Angular 2 1. Add Angular 2 routing library into your html file https://code.angularjs.org/2.0.0- alpha.45/router.dev.js <script src="https://code.angularjs.org/2.0.0-alpha.45/router.dev.js"></script> 2. Import all routing directive and classes from 'angular2/router' import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy} from 'angular2/router'; 3. Map URLs in @RouteConfig @RouteConfig([ {path: '/', component: HomeComponent,as:'Home'}, {path: '/aboutus',component:AboutUsComponent,as:'AboutUs' } {path: '/contactus',component: ContactUsComponent,as:'ContactUs' } ]) 4. In main root component use router-outlet where you want render components <router-outlet></router-outlet> 5. Define router-link for which component you want to show <router-link></router-link>. Plnkr - http://plnkr.co/edit/2de7CWiUrRMpl3wJgwbT?p=preview
  • 4. Routing Example in Angular 2 index.html <!DOCTYPE HTML> <HTML> <HEAD> <TITLE> UNDERSTAND ROUTING INANGULAR2</TITLE> <SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/TOOLS/SYSTEM.JS"></SCRIPT> <SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/TOOLS/TYPESCRIPT.JS"></SCRIPT> <SCRIPT SRC="CONFIG.JS"></SCRIPT> <SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/2.0.0-ALPHA.45/ANGULAR2.DEV.JS"></SCRIPT> <SCRIPT SRC="HTTPS://CODE.ANGULARJS.ORG/2.0.0-ALPHA.45/ROUTER.DEV.JS"></SCRIPT> <SCRIPT> SYSTEM.IMPORT('./APP.TS'); </SCRIPT> </HEAD> <BODY> <MAINROOT></MAINROOT> </BODY> </HTML>
  • 5. app.ts IMPORT {COMPONENT,BOOTSTRAP,PROVIDE} FROM 'ANGULAR2/ANGULAR2'; IMPORT {ROUTECONFIG, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, LOCATIONSTRATEGY,HASHLOCATIONSTRATEGY}FROM 'ANGULAR2/ROUTER'; //HOME COMPONENT @COMPONENT({ SELECTOR:'HOME', TEMPLATE:'<H2>WELCOME TO <A HREF="HTTP://WWW.CODEANDYOU.COM"TARGET="_BLANK"> WWW.CODEANDYOU.COM</A></H2>', STYLES:['.HOME {BACKGROUND:RED}'], }) EXPORT CLASS HOMECOMPONENT {} //ABOUT US COMPONENT @COMPONENT({ SELECTOR:'ABOUTUS', TEMPLATEURL:'ABOUTUS.HTML' }) EXPORT CLASS ABOUTUSCOMPONENT {} //CONTACT US COMPONENT @COMPONENT({ SELECTOR:'CONTACTUS', TEMPLATE:'<H2>CONTACT US</H2>' }) EXPORT CLASS CONTACTUSCOMPONENT {} //ROOT COMPONENT @COMPONENT({ SELECTOR:'MAINROOT', TEMPLATE:`<DIV CLASS="GRAYCOLOR"><A [ROUTER-LINK]="['/HOME']">HOME</A> <A [ROUTER-LINK]="['/ABOUTUS']">ABOUTUS</A> <A [ROUTER-LINK]="['/CONTACTUS']">CONTACT US</A> </DIV><HR> <ROUTER-OUTLET></ROUTER-OUTLET> `, DIRECTIVES:[ ROUTER_DIRECTIVES] }) @ROUTECONFIG([ {PATH:'/', COMPONENT: HOMECOMPONENT,AS:'HOME'}, {PATH:'/ABOUTUS', COMPONENT:ABOUTUSCOMPONENT,AS:'ABOUTUS' } {PATH:'/CONTACTUS',COMPONENT:CONTACTUSCOMPONENT,AS:'CONTACTUS' }
  • 6. ]) CLASS ROOTCOMPONENT{ } BOOTSTRAP(ROOTCOMPONENT,[ROUTER_PROVIDERS,PROVIDE(LOCATIONSTRATEGY,{USECLASS: HASHLOCATIONSTRATEGY})]); config.js SYSTEM.CONFIG({ //USE TYPESCRIPT FOR COMPILATION TRANSPILER:'TYPESCRIPT', //TYPESCRIPT COMPILER OPTIONS TYPESCRIPTOPTIONS:{ EMITDECORATORMETADATA:TRUE } }); aboutUs.html <H2>ABOUT US</H2>
  • 7. Thanks www.codeandyou.com http://www.codeandyou.com/2015/11/understand- routing-in-angular-2.html Keywords - Understand Routing in Angular 2, How to use Routing in Angular 2, routing in Angular 2, Angular 2 Routing