feat(router): extend support for lazy loading children (#10705)

This commit is contained in:
Victor Savkin
2016-08-15 21:11:09 -07:00
committed by vikerman
parent bec5c5fdad
commit 6b26102931
10 changed files with 84 additions and 25 deletions

View File

@ -6,12 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ComponentFactoryResolver, Injector, NgModuleFactoryLoader, OpaqueToken} from '@angular/core';
import {Compiler, ComponentFactoryResolver, Injector, NgModuleFactory, NgModuleFactoryLoader, OpaqueToken} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise';
import {of } from 'rxjs/observable/of';
import {LoadChildren, Route} from './config';
import {flatten, wrapIntoObservable} from './utils/collection';
import {Route} from './config';
import {flatten} from './utils/collection';
/**
@ -27,13 +29,24 @@ export class LoadedRouterConfig {
}
export class RouterConfigLoader {
constructor(private loader: NgModuleFactoryLoader) {}
constructor(private loader: NgModuleFactoryLoader, private compiler: Compiler) {}
load(parentInjector: Injector, path: string): Observable<LoadedRouterConfig> {
return fromPromise(this.loader.load(path).then(r => {
load(parentInjector: Injector, loadChildren: LoadChildren): Observable<LoadedRouterConfig> {
return this.loadModuleFactory(loadChildren).map(r => {
const ref = r.create(parentInjector);
return new LoadedRouterConfig(
flatten(ref.injector.get(ROUTES)), ref.injector, ref.componentFactoryResolver);
}));
});
}
}
private loadModuleFactory(loadChildren: LoadChildren): Observable<NgModuleFactory<any>> {
if (typeof loadChildren === 'string') {
return fromPromise(this.loader.load(loadChildren));
} else {
const offlineMode = this.compiler instanceof Compiler;
return wrapIntoObservable(loadChildren())
.mergeMap(
t => offlineMode ? of (<any>t) : fromPromise(this.compiler.compileModuleAsync(t)));
}
}
}