Ng2 Router
In index.html
<base href="/">
import { RouterModule } from '@angular/router';
RouterModule.forRoot([
{ path: 'hero/:id', component: HeroDetailComponent },
{ path: 'crisis-center', component: CrisisListComponent },
{
path: 'heroes',
component: HeroListComponent,
data: {
title: 'Heroes List'
}
},
{ path: '', component: HomeComponent },
{ path: '**', component: PageNotFoundComponent }
])
]
<!-- Routed views go here -->
<router-outlet></router-outlet>
|
RouterLink
|
The directive for binding a clickable HTML element to a route. Clicking an anchor tag with a routerLink directive that is bound to a string or a Link Parameters Array triggers a navigation.
|
|
RouterLinkActive
|
The directive for adding/removing classes from an HTML element when an associated routerLink contained on or inside the element becomes active/inactive.
|
|
ActivatedRoute
|
A service that is provided to each route component that contains route specific information such as route parameters, static data, resolve data, global query params and the global fragment.
|
|
RouterState
|
The current state of the router including a tree of the currently activated routes in our application along convenience methods for traversing the route tree.
|
|
Router
|
Displays the application component for the active URL. Manages navigation from one component to the next.
|
|
RouterModule
|
A separate Angular module that provides the necessary service providers and directives for navigating through application views.
|
|
Routes
|
Defines an array of Routes, each mapping a URL path to a component.
|
|
Route
|
Defines how the router should navigate to a component based on a URL pattern. Most routes consist of a path and a component type.
|
|
CanActivate
|
Checking route access
|
|
CanActivateChild
|
Checking child route access
|
|
CanDeactivate
|
Ask permission to discard unsaved changes
|
|
CanLoad
|
Check before loading feature module assets
|
RouterModule.forChild([
{ path: 'heroes', component: HeroListComponent },
{ path: 'hero/:id', component: HeroDetailComponent }
])
export function loadCompensationModule() {
return require('es6-promise!../cf/shell/shell.module')('ShellModule');
}
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full'},
{ path: 'cf', loadChildren: loadCompensationModule},
{ path: 'home', component: MainComponent, canActivate: [AuthGuard]},
{ path:'**', redirectTo: '' }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes, { useHash: false});
|
Route Parameter
|
xxx/:{type}
|
<a [routerLink]="['/crisis-center', { foo: 'foo' }]">Crisis Center</a>
|
PathLocationStrategy
|
the default "HTML 5 pushState" style.
|
|
HashLocationStrategy
|
the "hash URL" style.
|
import { Injectable } from '@angular/core';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.authService.isLoggedIn) { return true; }
// Store the attempted URL for redirecting
this.authService.redirectUrl = url;
// Navigate to the login page with extras
this.router.navigate(['/login']);
return false;
}
}
Import
import { Component, OnInit, HostBinding,
trigger, transition, animate,
style, state } from '@angular/core';
animations: [
trigger('routeAnimation', [
state('*',
style({
opacity: 1,
transform: 'translateX(0)'
})
),
transition('void => *', [
style({
opacity: 0,
transform: 'translateX(-100%)'
}),
animate('0.2s ease-in')
]),
transition('* => void', [
animate('0.5s ease-out', style({
opacity: 0,
transform: 'translateY(100%)'
}))
])
])
]
@HostBinding('@routeAnimation') get routeAnimation() {
return true;
}
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.