angular-docs-cn/modules/@angular/router/src/router_config_loader.ts

40 lines
1.2 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ComponentFactoryResolver, Injector, NgModuleFactoryLoader, OpaqueToken} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise';
import {Route} from './config';
import {flatten} from './utils/collection';
2016-07-06 19:19:52 -04:00
/**
* @deprecated use Routes
*/
2016-07-06 14:32:42 -04:00
export const ROUTER_CONFIG = new OpaqueToken('ROUTER_CONFIG');
2016-07-06 19:19:52 -04:00
export const ROUTES = new OpaqueToken('ROUTES');
2016-07-06 14:32:42 -04:00
export class LoadedRouterConfig {
constructor(
public routes: Route[], public injector: Injector,
public factoryResolver: ComponentFactoryResolver) {}
}
export class RouterConfigLoader {
constructor(private loader: NgModuleFactoryLoader) {}
load(parentInjector: Injector, path: string): Observable<LoadedRouterConfig> {
return fromPromise(this.loader.load(path).then(r => {
const ref = r.create(parentInjector);
return new LoadedRouterConfig(
flatten(ref.injector.get(ROUTES)), ref.injector, ref.componentFactoryResolver);
}));
}
}