by Gerard Sans (@gerardsans)
800
500
/home
/users
/about
/users/34
APP_BASE_HREF
import {RouteConfig} from 'angular2/router';
import {Home} from './home';
import {Users} from './users';
import {AboutLazyLoader} from './aboutLazyLoader';
@RouteConfig([
{ path: '/home', component: Home, name: 'Home', data: {level:0} },
{ path: '/users/...', component: Users, name: 'Users' },
{ path: '/about', loader: AboutLazyLoader , name: 'AboutLazyLoad' },
{ path: '/**', redirectTo: ['Users'] }
])
export class App { }
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
@Component({
selector: 'my-app',
template: `
<nav>...</nav>
<div>
<router-outlet></router-outlet>
</div>`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig(...)
export class App { }
/users/...
useAsDefault: true
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {UsersList} from './usersList';
import {UserDetails} from './userDetails';
@Component({
selector: 'users',
template: `<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{ path: '/', component: UsersList, name: 'UsersList', useAsDefault: true },
{ path: ':id', component: UserDetails, name: 'UserDetails' },
])
export class Users { }
/
UsersList
#/users, #/users/
:id
UserDetails
#/users/34
#/home
['Home']
['/Users/UserDetails', {id: 34}]
router.navigate(['Home']);
router.navigateByUrl('/home');
<!-- Top Routes -->
<a [routerLink]="['Home']">Home</a>
<a [routerLink]="['Users']">Users</a>
<a [routerLink]="['AboutLazyLoad']">About</a>
<!-- Nested Routes -->
<a [routerLink]="['/Users/UsersList']">Users</a>
<a [routerLink]="['/Users/UserDetails', {id: 34}]">Spiderman</a>
<a [routerLink]="['/Users/UserDetails', {id: 34, flag: true}]">Spiderman</a>
<!-- Relative Routes -->
<a [routerLink]="['./UsersList']">Users</a>
<a [routerLink]="['./UserDetails', {id: 34}]">Spiderman</a>
<a [routerLink]="['./UserDetails', {id: 34, flag: true}]">Spiderman</a>
@Component({
selector: 'my-app',
template: `
<router-outlet></router-outlet>
<router-outlet name="modal"></router-outlet>`,
})
@RouteConfig([
{path: '/hello', component: HelloCmp, name: 'Hello'}),
{aux: '/modal', component: ModalCmp, name: 'Modal'})
])
export class App { }
<a [routerLink]="['/Hello', ['Modal']]">both</a>
router.navigateByUrl('/hello(modal)'))
<a [routerLink]="['/', ['Modal']]">only modal</a>
router.navigateByUrl('/(modal)'))
/*
{ path: ':id', component: UserDetails, name: 'UserDetails' }
*/
import {RouteParams} from 'angular2/router';
@Component({
selector: 'user-details'
})
export class UserDetails {
constructor(private params: RouteParams){
let id = params.get('id');
}
}
/*
{ path: '/home', component: Home, name: 'Home', data: {level:0} }
*/
import {RouteData} from 'angular2/router';
@Component({
selector: 'home'
})
export class Home {
constructor(private data: RouteData){
console.log(data.get("level"));
}
}
Navigation from users list to user details
@Component({
selector: 'user-details'
})
export class UserDetails implements CanDeactivate {
routerCanDeactivate() {
return new Promise(resolve =>
resolve(window.confirm('Do you want to continue?'))
);
}
}
@Component({
selector: 'user-details'
})
@CanActivate(next => {
let injector = appInjector();
let service = injector.get(LoginService);
return new Promise(resolve => {
resolve(service.authorised);
});
})
export class UserDetails { }
@RouteConfig([
{ path: '/about', loader: AboutLazyLoader , name: 'AboutLazyLoad' }
])
export class App { }
//aboutLazyLoader.ts
export function AboutLazyLoader(){
return System.import('./src/aboutLazyLoad.ts')
.then(module => module.AboutLazyLoad);
}
//aboutLazyLoad.ts
@Component({
selector: 'about',
template: `<h1>About</h1>`
})
export class AboutLazyLoad { }
AngularConnect - Sept 27-28th
2x FREE Become a ninja with Angular 2
twit latest ng2 release full name to @gerardsans #rigadevday