This is a big change. @matsko also deserves much of the credit for the implementation. Previously, `ComponentInstruction`s held all the state for async components. Now, we introduce several subclasses for `Instruction` to describe each type of navigation. BREAKING CHANGE: Redirects now use the Link DSL syntax. Before: ``` @RouteConfig([ { path: '/foo', redirectTo: '/bar' }, { path: '/bar', component: BarCmp } ]) ``` After: ``` @RouteConfig([ { path: '/foo', redirectTo: ['Bar'] }, { path: '/bar', component: BarCmp, name: 'Bar' } ]) ``` BREAKING CHANGE: This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading and encapsulating large routes with sub-routes easier. Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`: @RouteConfig([ { path: '/tab', redirectTo: '/tab/users' } { path: '/tab', component: TabsCmp, name: 'Tab' } ]) AppCmp { ... } Now the recommended way to handle this is case is to use `useAsDefault` like so: ``` @RouteConfig([ { path: '/tab', component: TabsCmp, name: 'Tab' } ]) AppCmp { ... } @RouteConfig([ { path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' }, { path: '/users', component: UsersCmp, name: 'Users' } ]) TabsCmp { ... } ``` In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route. Closes #4728 Closes #4228 Closes #4170 Closes #4490 Closes #4694 Closes #5200 Closes #5475
29 lines
853 B
TypeScript
29 lines
853 B
TypeScript
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
|
import {isPresent, Type} from 'angular2/src/facade/lang';
|
|
|
|
import {RouteHandler} from './route_handler';
|
|
import {RouteData, BLANK_ROUTE_DATA} from './instruction';
|
|
|
|
|
|
export class AsyncRouteHandler implements RouteHandler {
|
|
/** @internal */
|
|
_resolvedComponent: Promise<any> = null;
|
|
componentType: Type;
|
|
public data: RouteData;
|
|
|
|
constructor(private _loader: Function, data: {[key: string]: any} = null) {
|
|
this.data = isPresent(data) ? new RouteData(data) : BLANK_ROUTE_DATA;
|
|
}
|
|
|
|
resolveComponentType(): Promise<any> {
|
|
if (isPresent(this._resolvedComponent)) {
|
|
return this._resolvedComponent;
|
|
}
|
|
|
|
return this._resolvedComponent = this._loader().then((componentType) => {
|
|
this.componentType = componentType;
|
|
return componentType;
|
|
});
|
|
}
|
|
}
|