SlideShare a Scribd company logo
Angular 2 Setup for Routing
Note : Before reading this see the setup of angular 2 document first for better
understanding follow the below link.
“ http://www.slideshare.net/goldsmithverma/anugula2setupbyshubham-
65904844 “
Step 1. 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.
<base href=“/“>
Step 2. Every application has a router. When we hit the new url, router looks for in
Route config and display the content of component for which route is mapped. We
need to configure the router, we bootstrap our app with an array of routes that is
provided by RouteModule.forRoot function.
create a routing file in your app directory
app/app.routing.ts
and write the code
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
const appRoutes: Routes = [
{
path: 'home',
component: HomeComponent
},
{
path: 'about',
component: AboutComponent,
data: {
title: 'About angular 2'
}
}
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
Step 3. We need to export the routing constant we can import into our
app.module.ts
// This is the entry point to your application
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { FormsModule } from '@angular/forms'
import { AppComponent } from './app.component';
import { routing, appRoutingProviders } from './app.routing';
import { AboutComponent } from './about/about.component';
import { HomeComponent } from './home/home.component';
@NgModule({
imports: [
BrowserModule,
FormsModule,
routing
],
declarations: [
AppComponent,
AboutComponent,
HomeComponent
],
providers: [
appRoutingProviders
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
Step 4. Configure the router outlet where the contents are fetched.
—> Routes views goes here
<router-outlet></router-outlet>
now change the contents of you app/app.component.html
with
<div>
<h1>Welcome to the world of angular 2</h1>
<nav>
<a [routerLink]="['/home']"> Home </a> | <a [routerLink]="['/about']">About</a>
</nav>
<router-outlet></router-outlet>
</div>
Step 5. Everything is done run the app
$ npm start
Shubham Verma
github: @codershubhamverma

More Related Content

What's hot

Understand routing in angular 2
Understand routing in angular 2Understand routing in angular 2
Understand routing in angular 2
codeandyou forums
 
Angular 2 Routing
Angular 2   RoutingAngular 2   Routing
Angular 2 Routing
Andres Ciceri
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
Xiaochun Shen
 
Pundit
PunditPundit
Pundit
Bruce White
 
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Atlassian
 
Gigigo Rails Workshop
Gigigo Rails WorkshopGigigo Rails Workshop
Gigigo Rails Workshop
Alex Rupérez
 
Einführung in Angular 2
Einführung in Angular 2Einführung in Angular 2
Einführung in Angular 2
Johannes Hoppe
 
Multiple Submit Button Test App
Multiple Submit Button Test AppMultiple Submit Button Test App
Multiple Submit Button Test App
Peeyush Ranjan
 
Ajax pagination using j query in rails3
Ajax pagination using j query in rails3Ajax pagination using j query in rails3
Ajax pagination using j query in rails3
Andolasoft Inc
 
ADF in action 1.2
ADF in action 1.2ADF in action 1.2
ADF in action 1.2
Eugenio Romano
 
Introduccion app engine con python
Introduccion app engine con pythonIntroduccion app engine con python
Introduccion app engine con python
sserrano44
 
Rack
RackRack
Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
William Leeper
 
Automation Zaman Now
Automation Zaman NowAutomation Zaman Now
Automation Zaman Now
Ibnu Fajar Yunardi
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
randyhoyt
 
Banquet 42
Banquet 42Banquet 42
Banquet 42
Koubei UED
 
Easy ota intro
Easy ota introEasy ota intro
Easy ota intro
Myungjin Choi
 
multiple views and routing
multiple views and routingmultiple views and routing
multiple views and routing
Brajesh Yadav
 
implementar Laravel make auth
implementar Laravel make authimplementar Laravel make auth
implementar Laravel make auth
Sebastian Romero
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwan
Tse-Ching Ho
 

What's hot (20)

Understand routing in angular 2
Understand routing in angular 2Understand routing in angular 2
Understand routing in angular 2
 
Angular 2 Routing
Angular 2   RoutingAngular 2   Routing
Angular 2 Routing
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Pundit
PunditPundit
Pundit
 
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
 
Gigigo Rails Workshop
Gigigo Rails WorkshopGigigo Rails Workshop
Gigigo Rails Workshop
 
Einführung in Angular 2
Einführung in Angular 2Einführung in Angular 2
Einführung in Angular 2
 
Multiple Submit Button Test App
Multiple Submit Button Test AppMultiple Submit Button Test App
Multiple Submit Button Test App
 
Ajax pagination using j query in rails3
Ajax pagination using j query in rails3Ajax pagination using j query in rails3
Ajax pagination using j query in rails3
 
ADF in action 1.2
ADF in action 1.2ADF in action 1.2
ADF in action 1.2
 
Introduccion app engine con python
Introduccion app engine con pythonIntroduccion app engine con python
Introduccion app engine con python
 
Rack
RackRack
Rack
 
Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
 
Automation Zaman Now
Automation Zaman NowAutomation Zaman Now
Automation Zaman Now
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
Banquet 42
Banquet 42Banquet 42
Banquet 42
 
Easy ota intro
Easy ota introEasy ota intro
Easy ota intro
 
multiple views and routing
multiple views and routingmultiple views and routing
multiple views and routing
 
implementar Laravel make auth
implementar Laravel make authimplementar Laravel make auth
implementar Laravel make auth
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwan
 

Viewers also liked

Apple's acquisition of beats what the doctor ordered
Apple's acquisition of beats what the doctor orderedApple's acquisition of beats what the doctor ordered
Apple's acquisition of beats what the doctor ordered
Jason Fernandes
 
как люди научились считать 6 класс 1
как люди научились считать 6 класс 1как люди научились считать 6 класс 1
как люди научились считать 6 класс 1
елена шапошникова
 
первый признак рав. треугольников
первый признак рав. треугольниковпервый признак рав. треугольников
первый признак рав. треугольников
елена шапошникова
 
15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS
15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS
15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS
Jason Fernandes
 
La era digital diana vanesa osorio sanch
La era digital diana vanesa osorio sanchLa era digital diana vanesa osorio sanch
La era digital diana vanesa osorio sanch
jesmidi
 
Google Glass, Project Aura and the Segway strategy
Google Glass, Project Aura and the Segway strategyGoogle Glass, Project Aura and the Segway strategy
Google Glass, Project Aura and the Segway strategy
Jason Fernandes
 
Stand up for the net - Mid-Day
Stand up for the net - Mid-Day Stand up for the net - Mid-Day
Stand up for the net - Mid-Day
Jason Fernandes
 
Account receivable clerk kpi
Account receivable clerk kpiAccount receivable clerk kpi
Account receivable clerk kpi
gkatgutos
 
Plant tissues
Plant tissuesPlant tissues
Plant tissues
rameesaharis
 
Can the internet of things survive the coming war
Can the internet of things survive the coming warCan the internet of things survive the coming war
Can the internet of things survive the coming war
Jason Fernandes
 
Executive account manager kpi
Executive account manager kpiExecutive account manager kpi
Executive account manager kpi
gkatgutos
 
Cyber Warfare: Can business trust the government to protect them?
Cyber Warfare: Can business trust the government to protect them?Cyber Warfare: Can business trust the government to protect them?
Cyber Warfare: Can business trust the government to protect them?
Jason Fernandes
 

Viewers also liked (12)

Apple's acquisition of beats what the doctor ordered
Apple's acquisition of beats what the doctor orderedApple's acquisition of beats what the doctor ordered
Apple's acquisition of beats what the doctor ordered
 
как люди научились считать 6 класс 1
как люди научились считать 6 класс 1как люди научились считать 6 класс 1
как люди научились считать 6 класс 1
 
первый признак рав. треугольников
первый признак рав. треугольниковпервый признак рав. треугольников
первый признак рав. треугольников
 
15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS
15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS
15-YEAR-OLD LAUNCHES WORLD-CLASS WEBSITE TO HELP OTHERS
 
La era digital diana vanesa osorio sanch
La era digital diana vanesa osorio sanchLa era digital diana vanesa osorio sanch
La era digital diana vanesa osorio sanch
 
Google Glass, Project Aura and the Segway strategy
Google Glass, Project Aura and the Segway strategyGoogle Glass, Project Aura and the Segway strategy
Google Glass, Project Aura and the Segway strategy
 
Stand up for the net - Mid-Day
Stand up for the net - Mid-Day Stand up for the net - Mid-Day
Stand up for the net - Mid-Day
 
Account receivable clerk kpi
Account receivable clerk kpiAccount receivable clerk kpi
Account receivable clerk kpi
 
Plant tissues
Plant tissuesPlant tissues
Plant tissues
 
Can the internet of things survive the coming war
Can the internet of things survive the coming warCan the internet of things survive the coming war
Can the internet of things survive the coming war
 
Executive account manager kpi
Executive account manager kpiExecutive account manager kpi
Executive account manager kpi
 
Cyber Warfare: Can business trust the government to protect them?
Cyber Warfare: Can business trust the government to protect them?Cyber Warfare: Can business trust the government to protect them?
Cyber Warfare: Can business trust the government to protect them?
 

Similar to Angular2RoutingSetupByShubham

Angular routing
Angular routingAngular routing
Angular routing
Sultan Ahmed
 
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
Frost
 
Angular 2
Angular 2Angular 2
ngNewRouter
ngNewRouterngNewRouter
ngNewRouter
phidong
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in Angular
Neoito
 
Peggy angular 2 in meteor
Peggy   angular 2 in meteorPeggy   angular 2 in meteor
Peggy angular 2 in meteor
LearningTech
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
Sebastian Holstein
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2
Ivan Matiishyn
 
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
Maurice De Beijer [MVP]
 
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
Maurice De Beijer [MVP]
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!
Web Standards School
 
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
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
Eyal Vardi
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
Bipin
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
Christoffer Noring
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2Commit University - Exploring Angular 2
Commit University - Exploring Angular 2
Commit University
 
Angular2 routing
Angular2 routingAngular2 routing
Angular2 routing
TejinderMakkar
 
Angular components
Angular componentsAngular components
Angular components
Sultan Ahmed
 

Similar to Angular2RoutingSetupByShubham (20)

Angular routing
Angular routingAngular routing
Angular routing
 
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
 
Angular 2
Angular 2Angular 2
Angular 2
 
ngNewRouter
ngNewRouterngNewRouter
ngNewRouter
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in Angular
 
Peggy angular 2 in meteor
Peggy   angular 2 in meteorPeggy   angular 2 in meteor
Peggy angular 2 in meteor
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2
 
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
 
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
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
 
هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!هیبرید کارا، از رویا تا واقعیت!
هیبرید کارا، از رویا تا واقعیت!
 
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!
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2Commit University - Exploring Angular 2
Commit University - Exploring Angular 2
 
Angular2 routing
Angular2 routingAngular2 routing
Angular2 routing
 
Angular components
Angular componentsAngular components
Angular components
 

Angular2RoutingSetupByShubham

  • 1. Angular 2 Setup for Routing Note : Before reading this see the setup of angular 2 document first for better understanding follow the below link. “ http://www.slideshare.net/goldsmithverma/anugula2setupbyshubham- 65904844 “ Step 1. 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. <base href=“/“> Step 2. Every application has a router. When we hit the new url, router looks for in Route config and display the content of component for which route is mapped. We need to configure the router, we bootstrap our app with an array of routes that is provided by RouteModule.forRoot function. create a routing file in your app directory app/app.routing.ts and write the code import { Routes, RouterModule } from '@angular/router'; import { ModuleWithProviders } from '@angular/core'; import { AboutComponent } from './about/about.component'; import { HomeComponent } from './home/home.component'; const appRoutes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent, data: { title: 'About angular 2' } } ]; export const appRoutingProviders: any[] = [ ]; export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
  • 2. Step 3. We need to export the routing constant we can import into our app.module.ts // This is the entry point to your application import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { APP_BASE_HREF } from '@angular/common'; import { RouterModule } from '@angular/router'; import { HttpModule } from '@angular/http'; import { FormsModule } from '@angular/forms' import { AppComponent } from './app.component'; import { routing, appRoutingProviders } from './app.routing'; import { AboutComponent } from './about/about.component'; import { HomeComponent } from './home/home.component'; @NgModule({ imports: [ BrowserModule, FormsModule, routing ], declarations: [ AppComponent, AboutComponent, HomeComponent ], providers: [ appRoutingProviders ], bootstrap: [ AppComponent ] }) export class AppModule { }
  • 3. Step 4. Configure the router outlet where the contents are fetched. —> Routes views goes here <router-outlet></router-outlet> now change the contents of you app/app.component.html with <div> <h1>Welcome to the world of angular 2</h1> <nav> <a [routerLink]="['/home']"> Home </a> | <a [routerLink]="['/about']">About</a> </nav> <router-outlet></router-outlet> </div> Step 5. Everything is done run the app $ npm start Shubham Verma github: @codershubhamverma