17.10.2018 Views

Angular

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Basic precaution: Ensure you have the tag<br />

<br />

as the first child under your head tag in your index.html file. This tag tells that your app folder is the application<br />

root. <strong>Angular</strong> 2 would then know to organize your links.<br />

First step is to check if you are pointing to correct/latest routing dependencies in package.json -<br />

"dependencies": {<br />

......<br />

"@angular/router": "3.0.0-beta.1",<br />

......<br />

}<br />

Second step is to define the route as per it's class definition -<br />

class Route {<br />

path : string<br />

pathMatch : 'full'|'prefix'<br />

component : Type|string<br />

.........<br />

}<br />

In a routes file (route/routes.ts), import all the components which you need to configure for different routing<br />

paths. Empty path means that view is loaded by default. ":" in the path indicates dynamic parameter passed to the<br />

loaded component.<br />

Routes are made available to application via dependency injection. ProviderRouter method is called with<br />

RouterConfig as parameter so that it can be injected to the components for calling routing specific tasks.<br />

import { provideRouter, RouterConfig } from '@angular/router';<br />

import { BarDetailComponent } from '../components/bar-detail.component';<br />

import { DashboardComponent } from '../components/dashboard.component';<br />

import { LoginComponent } from '../components/login.component';<br />

import { SignupComponent } from '../components/signup.component';<br />

export const appRoutes: RouterConfig = [<br />

{ path: '', pathMatch: 'full', redirectTo: 'login' },<br />

{ path: 'dashboard', component: DashboardComponent },<br />

{ path: 'bars/:id', component: BarDetailComponent },<br />

{ path: 'login', component: LoginComponent },<br />

{ path: 'signup', component: SignupComponent }<br />

];<br />

export const APP_ROUTER_PROVIDER = [provideRouter(appRoutes)];<br />

Third step is to bootstrap the route provider.<br />

In your main.ts (It can be any name. basically, it should your main file defined in systemjs.config)<br />

import { bootstrap } from '@angular/platform-browser-dynamic';<br />

import { AppComponent } from './components/app.component';<br />

import { APP_ROUTER_PROVIDER } from "./routes/routes";<br />

bootstrap(AppComponent, [ APP_ROUTER_PROVIDER ]).catch(err => console.error(err));<br />

GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 76

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!