angular 2.0 routing and navigation

25
Angular 2.0 Routing & Navigation Eyal Vard Site: http://ng-course.o Blog: eyalVardi.wordpress.

Upload: eyal-vardi

Post on 09-Jan-2017

1.284 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Angular 2.0 Routing and Navigation

Angular 2.0 Routing & Navigation

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com

Page 2: Angular 2.0 Routing and Navigation

Agenda Installation & Setup RouteConfig Decorator RouterOutlet & RouterLink Directives Link Parameters Array RouteParams & RouteData Services Router Lifecycle Hooks

Page 3: Angular 2.0 Routing and Navigation

Index.html Setup Add the angular router script.

<script src="router.dev.js"></script>

import { RouteConfig, ROUTER_PROVIDERS, ROUTER_DIRECTIVES , HashLocationStrategy, LocationStrategy} from 'angular2/router';

Page 4: Angular 2.0 Routing and Navigation

Installationimport {bootstrap} from 'angular2/platform/browser';import { ROUTER_PROVIDERS, ROUTER_DIRECTIVES from 'angular2/router';

bootstrap(AppComponent, [ ROUTER_PROVIDERS]);

@Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>', directives: [ROUTER_DIRECTIVES]})export class AppComponent { }

Page 5: Angular 2.0 Routing and Navigation

Routing Strategies HashLocationStrategy ('#/') PathLocationStrategy (HTML 5 Mode)import {bootstrap} from 'angular2/platform/browser';import { ROUTER_PROVIDERS,ROUTER_DIRECTIVES, HashLocationStrategy, LocationStrategy} from 'angular2/router';

bootstrap(AppComponent, [ ROUTER_PROVIDERS, provide(LocationStrategy, {useClass:HashLocationStrategy})]);

Page 6: Angular 2.0 Routing and Navigation

Path Location Strategy (HTML 5 Mode) Must set base URL. How to set the base URL, 3 options:

bootstrap( App, [ ROUTER_PROVIDERS, provide( APP_BASE_HREF , { useValue: '/' }) ]);

<script> document.write( '<base href="' + document.location + '" />');</script>

<base href="/" >1.

2.

3.

Page 7: Angular 2.0 Routing and Navigation

RouterLink & RouterOutlet

<router-outlet>

<a [routerLink]="['Go']">Go</a>

Code : router.navigate( ['Go'] );

HTML : <a [routerLink]="['Go']">Go</a>

Component:

Template

Page 8: Angular 2.0 Routing and Navigation

Route Definition path - the URL path segment for this

route. name - the name of the route. component - the Component associated with

this route. useAsDefault - makes this route the default

route. redirectTo@Component({ ... })@RouteConfig([ {path:'/home‘ , name: 'Home' , component: Home}, {path:'/products' , name: 'Products', component: Products}, {path:'/users' , name: 'Users' , component: Users} ]) export class AppComponent { }

Page 9: Angular 2.0 Routing and Navigation

Url : http://ev.com/

<router-outlet>

Component App

Home | Products | Users |

<a [routerLink]="[ '

Page 10: Angular 2.0 Routing and Navigation

Url : http://ev.com/

<router-outlet>

Component App

Home | Products | Users |

Products

<router-outlet>

Product 1

Product 2

Product 3

Product 4

Product 5

Product 6

Product 7

Product 8

products/

<a [routerLink]="[ '

products ',

Page 11: Angular 2.0 Routing and Navigation

Url : http://ev.com/

<router-outlet>

Component App

Home | Products | Users |

Products

<router-outlet>

Product 1

Product 2

Product 3

Product 4

Product 5

Product 6

Product 7

Product 8

products/product/1

<router-outlet>

Details | Price | Spec |

<a [routerLink]="[ '

products ','product',{id:1}]">

Page 12: Angular 2.0 Routing and Navigation

Route Definition@Component({ ... })@RouteConfig([ {path:'/home' , name: 'Home' , component: HomeComp}, {path:'/products/...' , name: 'Products', component: ProductsComp}, {path:'/Users' , name: 'Users' , component: UsersComp} ])export class AppComponent { }

@Component({ ... })@RouteConfig([ {path:'/product/:id/...',name: 'Product', component: ProductComp}])export class ProductsComponent { }

@Component({ ... })@RouteConfig([ {path:'/Details', name: 'Details',component: DetailsComp}, {path:'/Price' , name: 'Price' ,component: PriceComp}, {path:'/Spec' , name: 'Spec' ,component: SpecComp} ])export class ProductComponent { }

Page 13: Angular 2.0 Routing and Navigation

App

Products

Product

Link Parameters Array

['Products']

[routerLink]

['Products','Product']

[routerLink]

['./Product']

[routerLink]

[routerLink]

['/Products','Product']

Page 14: Angular 2.0 Routing and Navigation

Query Parameters We use route parameters to specify a

parameterized value within the route URL. URL query Route parameters object{path:'/:id', name:'Product', component:Product}

<a [routerLink]="['Product', {id:1}]">Product 1</a>

router.navigate(['Product' ,'{id:8}']);

Page 15: Angular 2.0 Routing and Navigation

RouteParams

@Component({ template: 'user: {{id}}' }) class UserCmp { id: string; constructor(params: RouteParams) { this.id = params.get('id'); } }

Page 16: Angular 2.0 Routing and Navigation

RouteData@Component({...})@View({directives: [ROUTER_DIRECTIVES]})@RouteConfig([{ path: '/user/:id' , component: UserCmp , as: 'UserCmp' , data: {isAdmin: true}}])class AppCmp {}

@Component({ template: 'user: {{isAdmin}}' })class UserCmp { string: isAdmin; constructor(data: RouteData) { this.isAdmin = data.get('isAdmin'); } }

Page 17: Angular 2.0 Routing and Navigation

Instructionimport {Component} from 'angular2/core';import {Router,ROUTER_DIRECTIVES,RouteConfig} from 'angular2/router';

@Component({directives: [ROUTER_DIRECTIVES]})@RouteConfig([{...}])class AppCmp { constructor(router: Router) {

var instruction = router.generate(['/MyRoute']); router.navigateByInstruction(instruction);

}}

Page 18: Angular 2.0 Routing and Navigation

Matrix URL Notation Matrix URL notation is an idea first floated in a 1996

proposal by the founder of the web, Tim Berners-Lee.

Although matrix notation never made it into the HTML standard, it is legal and it became popular among browser routing systems as a way to isolate parameters belonging to parent and child routes.

<a [routerLink]="['Product', {id:1,foo:3}]">Product 1</a>

localhost:3000/product/;id=1;foo=foo

Page 19: Angular 2.0 Routing and Navigation

Router Lifecycle Hooks

Page 20: Angular 2.0 Routing and Navigation

Router Lifecycle Hooks

canDeactivate

OnDeactivate

OnActivate

['Products'][routerLink]

CanActivate

Page 21: Angular 2.0 Routing and Navigation

@CanActivate Decorator The @CanActivate function is called

before the component is instantiated, Must be an annotation.@CanActivate((next, prev) => boolean | Promise<boolean>)

Sample:

@Component({selector: 'control-panel-cmp‘, template: `<div>Settings: ...</div>`

})@CanActivate(checkIfWeHavePermission)class ControlPanelCmp {}

Page 22: Angular 2.0 Routing and Navigation

CanReuse & OnReuse Determine whether a component should

be reused across routes, or whether to destroy and instantiate a new component. Use URL Param’s to load right object.

canReuse

OnReuse

[routerLink]

Page 23: Angular 2.0 Routing and Navigation

View I

Cancelling Route Changes

View IICancel

OK

routerCanDeactivate( next: ComponentInstruction, prev: ComponentInstruction) : any { return new Promise<boolean>((resolve, reject) => resolve(window.confirm('Continue?')));}

Page 24: Angular 2.0 Routing and Navigation

Lazy Loading (AsyncRoute class) AsyncRoute is a type of RouteDefinition

used to route a path to an asynchronously loaded component.

@RouteConfig([ new AsyncRoute({ path: '/home', loader: () => System.import('./Home').then(m=>m['Home']), name: 'Home' }), {path:'/users/...', name: 'Users', component: Users}])export class App { }

Page 25: Angular 2.0 Routing and Navigation

Thankseyalvardi.wordpress.com

Eyal Vardi

Site: http://ng-course.org

Blog: eyalVardi.wordpress.com