2015-11-06 07:26:24 -05:00
@cheatsheetSection
Routing and navigation
2016-08-08 20:18:50 -04:00
@cheatsheetIndex 11
2015-11-06 07:26:24 -05:00
@description
2016-08-08 20:25:42 -04:00
{@target ts}`import { Routes, RouterModule, ... } from '@angular/router';`{@endtarget}
2015-12-12 22:17:26 -05:00
{@target js}Available from the `ng.router` namespace{@endtarget}
2016-04-01 16:03:10 -04:00
{@target dart}`import 'package:angular2/router.dart';`{@endtarget}
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2015-12-12 22:17:26 -05:00
syntax(ts):
2016-08-08 21:30:25 -04:00
`const routes: Routes = [
2016-08-19 18:48:09 -04:00
{ path: '', component: HomeComponent },
2016-08-08 20:25:42 -04:00
{ path: 'path/:routeParam', component: MyComponent },
{ path: 'staticPath', component: ... },
{ path: '**', component: ... },
{ path: 'oldPath', redirectTo: '/staticPath' },
{ path: ..., component: ..., data: { message: 'Custom' } }
]);
2016-08-08 21:30:25 -04:00
const routing = RouterModule.forRoot(routes);`|`Routes`
2015-12-12 22:17:26 -05:00
syntax(js):
2016-08-08 20:25:42 -04:00
`var routes = [
2016-08-19 18:48:09 -04:00
{ path: '', component: HomeComponent },
2016-08-08 20:25:42 -04:00
{ path: ':routeParam', component: MyComponent },
{ path: 'staticPath', component: ... },
{ path: '**', component: ... },
{ path: 'oldPath', redirectTo: '/staticPath' },
{ path: ..., component: ..., data: { message: 'Custom' } }
]);
2016-08-08 21:30:25 -04:00
var routing = ng.router.RouterModule.forRoot(routes);`|`ng.router.Routes`
2015-12-09 13:22:40 -05:00
syntax(dart):
`@RouteConfig(const [
2016-08-19 18:48:09 -04:00
const Route(path: '', component: HomeComponent, name: 'Home'),
const Route(path: ':routeParam', component: MyComponent, name: 'MyCmp'),
const Route(path: 'staticPath', component: ..., name: 'StaticCmp'),
const Route(path: '**', component: ..., name: 'WildcardCmp'),
const Route(path: '/oldPath', redirectTo: ['/StaticCmp']),
const Route(path: ..., component: ..., data: { message: 'Custom' })
])class MyComponent() {}`|`@RouteConfig`
2015-12-09 07:33:42 -05:00
description:
2016-08-19 18:48:09 -04:00
Configures routes for the application. Supports static, parameterized, redirect and wildcard routes. Also supports custom route data{@target ts js} and resolve{@endtarget}.
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2015-12-09 07:33:42 -05:00
syntax:
2016-08-19 18:48:09 -04:00
`
< router-outlet > < / router-outlet >
< router-outlet name = "aux" > < / router-outlet >
`|` router-outlet`
2015-12-09 07:33:42 -05:00
description:
2015-11-06 07:26:24 -05:00
Marks the location to load the component of the active route.
@cheatsheetItem
2016-08-08 20:25:42 -04:00
syntax(ts js):
`
2016-08-08 21:30:25 -04:00
< a routerLink = "/path" >
2016-08-08 20:25:42 -04:00
< a [ routerLink ] = " [ ' / path ' , routeParam ] " >
< a [ routerLink ] = " [ ' / path ' , { matrixParam: ' value ' } ] " >
< a [ routerLink ] = " [ ' / path ' ] " [ queryParams ] = " { page: 1 } " >
< a [ routerLink ] = " [ ' / path ' ] " fragment = "anchor" >
`|` [routerLink]`
syntax(dart):
2016-08-19 18:48:09 -04:00
`
< a [ routerLink ] = " [ ' / MyCmp ' , { routeParam: ' value ' } ] " >
< a [ routerLink ] = " [ ' / MyCmp ' , { matrixParam: ' value ' } ] " >
`|` [routerLink]`
2015-12-09 07:33:42 -05:00
description:
2016-08-08 20:25:42 -04:00
Creates a link to a different view based on a route instruction consisting of a route path, required and optional parameters, query parameters and a fragment. Add the '/' prefix to navigate to a root route; add the './' prefix for a child route; add the '../sibling' prefix for a sibling or parent route.
2015-11-06 07:26:24 -05:00
2016-08-08 20:25:42 -04:00
@cheatsheetItem
syntax(ts js):
`<a [routerLink]="[ '/path' ]" routerLinkActive="active">`
syntax(dart):
2016-08-19 18:48:09 -04:00
`<a [routerLink]="[ '/MyCmp', { myParam: 'value' } ]">` |`routerLink`
2016-08-08 20:25:42 -04:00
description:
The provided class(es) will be added to the element when the routerLink becomes the current active route.
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2015-12-12 22:17:26 -05:00
syntax(ts):
2016-08-08 20:25:42 -04:00
`class CanActivateGuard implements CanActivate {
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
2016-08-19 18:48:09 -04:00
): Observable< boolean > |Promise< boolean > |boolean { ... }
2016-08-08 20:25:42 -04:00
}
{ path: ..., canActivate: [CanActivateGuard] }`|`CanActivate`
2015-12-12 22:17:26 -05:00
syntax(js):
2016-08-08 20:25:42 -04:00
`var CanActivateGuard = ng.core.Class({
canActivate: function(route, state) {
2016-08-19 18:48:09 -04:00
// return Observable/Promise boolean or boolean
2016-08-08 20:25:42 -04:00
}
});
{ path: ..., canActivate: [CanActivateGuard] }`|`CanActivate`
2015-12-09 13:22:40 -05:00
syntax(dart):
`@CanActivate(() => ...)class MyComponent() {}` |`@CanActivate`
2015-12-09 07:33:42 -05:00
description:
2016-08-19 18:48:09 -04:00
{@target js ts}An interface for defining a class that the router should call first to determine if it should activate this component. Should return an Observable/Promise that resolves a boolean or a boolean{@endtarget}
2016-08-08 20:25:42 -04:00
{@target dart}A component decorator defining a function that the router should call first to determine if it should activate this component. Should return a boolean or a future.{@endtarget}
@cheatsheetItem
syntax(ts):
`class CanDeactivateGuard implements CanDeactivate< T > {
canDeactivate(
component: T,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
2016-08-19 18:48:09 -04:00
): Observable< boolean > |Promise< boolean > |boolean { ... }
2016-08-08 20:25:42 -04:00
}
{ path: ..., canDeactivate: [CanDeactivateGuard] }`|`CanDeactivate`
syntax(js):
`var CanDeactivateGuard = ng.core.Class({
canDeactivate: function(component, route, state) {
2016-08-19 18:48:09 -04:00
// return Observable/Promise boolean or boolean
2016-08-08 20:25:42 -04:00
}
});
{ path: ..., canDeactivate: [CanDeactivateGuard] }`|`CanDeactivate`
syntax(dart):
`routerCanDeactivate(nextInstruction, prevInstruction) { ... }` |`routerCanDeactivate`
description:
2016-08-19 18:48:09 -04:00
{@target js ts}An interface for defining a class that the router should call first to determine if it should deactivate this component after a navigation. Should return an Observable/Promise that resolves a boolean or a boolean{@endtarget}
2016-08-08 20:25:42 -04:00
{@target dart}
The router calls the routerCanDeactivate methods (if defined) of every component that would be removed after a navigation. The navigation proceeds if and only if all such methods return true or a future that completes successfully{@endtarget}.
2015-11-06 07:26:24 -05:00
2016-08-19 18:48:09 -04:00
@cheatsheetItem
syntax(ts):
`class CanActivateChildGuard implements CanActivateChild {
canActivateChild(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable< boolean > |Promise< boolean > |boolean { ... }
}
{ path: ..., canActivateChild: [CanActivateGuard], children: ... }`|`CanActivateChild`
syntax(js):
`var CanActivateChildGuard = ng.core.Class({
canActivateChild: function(route, state) {
// return Observable/Promise boolean or boolean
}
});
{ path: ..., canActivateChild: [CanActivateChildGuard], children: ... }`|`CanActivateChild`
description:
{@target js ts}An interface for defining a class that the router should call first to determine if it should activate the child route. Should return an Observable/Promise that resolves a boolean or a boolean{@endtarget}
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2016-08-08 20:25:42 -04:00
syntax(ts):
`class ResolveGuard implements Resolve< T > {
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
2016-08-19 18:48:09 -04:00
): Observable< any > |Promise< any > |any { ... }
2016-08-08 20:25:42 -04:00
}
{ path: ..., resolve: [ResolveGuard] }`|`Resolve`
2015-12-12 22:17:26 -05:00
syntax(js):
2016-08-08 20:25:42 -04:00
`var ResolveGuard = ng.core.Class({
resolve: function(route, state) {
2016-08-19 18:48:09 -04:00
// return Observable/Promise value or value
2016-08-08 20:25:42 -04:00
}
});
{ path: ..., resolve: [ResolveGuard] }`|`Resolve`
description:
2016-08-19 18:48:09 -04:00
{@target js ts}An interface for defining a class that the router should call first to resolve route data before rendering the route. Should return an Observable/Promise that resolves a value or a value{@endtarget}
@cheatsheetItem
syntax(ts):
`class CanLoadGuard implements CanLoad {
canLoad(
route: Route
): Observable< boolean > |Promise< boolean > |boolean { ... }
}
{ path: ..., canLoad: [CanLoadGuard], loadChildren: ... }`|`CanLoad`
syntax(js):
`var CanLoadGuard = ng.core.Class({
canLoad: function(route) {
// return Observable/Promise boolean or boolean
}
});
{ path: ..., canLoad: [CanLoadGuard], loadChildren: ... }`|`CanLoad`
description:
{@target js ts}An interface for defining a class that the router should call first to check if the lazy loaded module should be loaded. Should return an Observable/Promise that resolves a boolean or a boolean{@endtarget}
2016-08-08 20:25:42 -04:00
@cheatsheetItem
syntax(dart):
`routerOnActivate(nextInstruction, prevInstruction) { ... }` |`routerOnActivate`
2015-12-09 07:33:42 -05:00
description:
2016-08-08 20:25:42 -04:00
{@target dart}After navigating to a component, the router calls the component's routerOnActivate method (if defined).{@endtarget}
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2016-08-08 20:25:42 -04:00
syntax(dart):
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 20:04:36 -05:00
`routerCanReuse(nextInstruction, prevInstruction) { ... }` |`routerCanReuse`
2015-12-09 07:33:42 -05:00
description:
2016-08-08 20:25:42 -04:00
{@target dart}The router calls a component's routerCanReuse method (if defined) to determine whether to reuse the instance or destroy it and create a new instance. Should return a boolean or a future{@endtarget}.
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2016-08-08 20:25:42 -04:00
syntax(dart):
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 20:04:36 -05:00
`routerOnReuse(nextInstruction, prevInstruction) { ... }` |`routerOnReuse`
2015-12-09 07:33:42 -05:00
description:
2016-08-08 20:25:42 -04:00
{@target dart}The router calls the component's routerOnReuse method (if defined) when it re-uses a component instance.{@endtarget}
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2016-08-08 20:25:42 -04:00
syntax(dart):
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 20:04:36 -05:00
`routerCanDeactivate(nextInstruction, prevInstruction) { ... }` |`routerCanDeactivate`
2015-12-09 07:33:42 -05:00
description:
2016-08-08 20:25:42 -04:00
{@target dart}The router calls the routerCanDeactivate methods (if defined) of every component that would be removed after a navigation. The navigation proceeds if and only if all such methods return true or a future that completes successfully.{@endtarget}
2015-11-06 07:26:24 -05:00
@cheatsheetItem
2016-08-08 20:25:42 -04:00
syntax(dart):
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 20:04:36 -05:00
`routerOnDeactivate(nextInstruction, prevInstruction) { ... }` |`routerOnDeactivate`
2015-12-09 07:33:42 -05:00
description:
2016-08-19 18:48:09 -04:00
{@target dart}Called before the directive is removed as the result of a route change. May return a future that pauses removing the directive until the future completes.{@endtarget}