angular-cn/modules/@angular/router/src/router_app_module.ts

69 lines
2.2 KiB
TypeScript
Raw Normal View History

2016-07-06 18:36:50 -04:00
/**
* @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 {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
2016-07-06 19:19:52 -04:00
import {AppModule, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
2016-07-06 18:36:50 -04:00
2016-07-06 19:19:52 -04:00
import {ROUTER_CONFIGURATION, setupRouter} from './common_router_providers';
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
import {RouterLinkActive} from './directives/router_link_active';
import {RouterOutlet} from './directives/router_outlet';
2016-07-06 18:36:50 -04:00
import {Router} from './router';
2016-07-06 19:19:52 -04:00
import {ROUTES} from './router_config_loader';
2016-07-06 18:36:50 -04:00
import {RouterOutletMap} from './router_outlet_map';
import {ActivatedRoute} from './router_state';
import {DefaultUrlSerializer, UrlSerializer} from './url_tree';
/**
* @stable
*/
export const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkWithHref, RouterLinkActive];
/**
* Router module.
*
* ### Example
*
* ```
* bootstrap(AppCmp, {modules: [RouterAppModule]});
* ```
*
* @experimental
*/
@AppModule({
directives: ROUTER_DIRECTIVES,
providers: [
Location, {provide: LocationStrategy, useClass: PathLocationStrategy},
2016-07-06 19:19:52 -04:00
{provide: UrlSerializer, useClass: DefaultUrlSerializer}, {
2016-07-06 18:36:50 -04:00
provide: Router,
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
2016-07-06 19:19:52 -04:00
AppModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
2016-07-06 18:36:50 -04:00
]
},
RouterOutletMap,
{provide: ActivatedRoute, useFactory: (r: Router) => r.routerState.root, deps: [Router]},
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader},
2016-07-06 19:19:52 -04:00
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
2016-07-06 18:36:50 -04:00
]
})
export class RouterAppModule {
constructor(private injector: Injector) {
setTimeout(() => {
const appRef = injector.get(ApplicationRef);
if (appRef.componentTypes.length == 0) {
appRef.registerBootstrapListener(() => { injector.get(Router).initialNavigation(); });
} else {
injector.get(Router).initialNavigation();
}
}, 0);
}
}