Skip to content

Commit ee32b1b

Browse files
committed
feat(router): allow async routes to be defined with "loader"
1 parent 6d4bd5d commit ee32b1b

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

modules/angular2/src/router/route_config_nomalizer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ export function normalizeRouteConfig(config: RouteDefinition): RouteDefinition {
1313
return <RouteDefinition>config;
1414
}
1515

16-
if ((!config.component) == (!config.redirectTo)) {
16+
if ((+!!config.component) + (+!!config.redirectTo) + (+!!config.loader) != 1) {
1717
throw new BaseException(
1818
`Route config should contain exactly one "component", "loader", or "redirectTo" property.`);
1919
}
20+
if (config.loader) {
21+
return new AsyncRoute({path: config.path, loader: config.loader, as: config.as});
22+
}
2023
if (config.component) {
2124
if (typeof config.component == 'object') {
2225
let componentDefinitionObject = <ComponentDefinition>config.component;

modules/angular2/test/router/route_config_spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ export function main() {
100100
}));
101101

102102

103+
it('should work in an app with async components defined with "loader"',
104+
inject([AsyncTestCompleter], (async) => {
105+
bootstrap(ConciseAsyncAppCmp,
106+
[bind(ROUTER_PRIMARY_COMPONENT).toValue(AsyncAppCmp), testBindings])
107+
.then((applicationRef) => {
108+
var router = applicationRef.hostComponent.router;
109+
router.subscribe((_) => {
110+
expect(el).toHaveText('root { hello }');
111+
expect(applicationRef.hostComponent.location.path()).toEqual('/hello');
112+
async.done();
113+
});
114+
router.navigateByUrl('/hello');
115+
});
116+
}));
117+
118+
103119
it('should work in an app with a constructor component',
104120
inject([AsyncTestCompleter], (async) => {
105121
bootstrap(
@@ -187,6 +203,15 @@ class AsyncAppCmp {
187203
constructor(public router: Router, public location: LocationStrategy) {}
188204
}
189205

206+
@Component({selector: 'app-cmp'})
207+
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
208+
@RouteConfig([
209+
{path: '/hello', loader: HelloLoader},
210+
])
211+
class ConciseAsyncAppCmp {
212+
constructor(public router: Router, public location: LocationStrategy) {}
213+
}
214+
190215
@Component({selector: 'app-cmp'})
191216
@View({template: `root { <router-outlet></router-outlet> }`, directives: ROUTER_DIRECTIVES})
192217
@RouteConfig([

0 commit comments

Comments
 (0)