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

105 lines
3.5 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 {APP_BASE_HREF, HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
import {ApplicationRef, Compiler, ComponentResolver, Inject, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, Optional, SystemJsNgModuleLoader} from '@angular/core';
2016-07-06 18:36:50 -04:00
import {ExtraOptions, ROUTER_CONFIGURATION, provideRouterConfig, provideRouterInitializer, provideRoutes, rootRoute, setupRouter} from './common_router_providers';
import {Routes} from './config';
2016-07-06 19:19:52 -04:00
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';
2016-07-06 18:36:50 -04:00
/**
* @stable
*/
export const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkWithHref, RouterLinkActive];
const pathLocationStrategy = {
provide: LocationStrategy,
useClass: PathLocationStrategy
};
const hashLocationStrategy = {
provide: LocationStrategy,
useClass: HashLocationStrategy
};
2016-07-07 17:13:32 -04:00
export const ROUTER_PROVIDERS: any[] = [
Location, {provide: UrlSerializer, useClass: DefaultUrlSerializer}, {
2016-07-07 17:13:32 -04:00
provide: Router,
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
NgModuleFactoryLoader, Compiler, ROUTES, ROUTER_CONFIGURATION
2016-07-07 17:13:32 -04:00
]
},
RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
{provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader},
2016-07-07 17:13:32 -04:00
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
];
2016-07-06 18:36:50 -04:00
/**
* Router module.
*
* When registered at the root, it should be used as follows:
*
* ### Example
*
* ```
* bootstrap(AppCmp, {imports: [RouterModule.forRoot(ROUTES)]});
* ```
*
* For lazy loaded modules it should be used as follows:
2016-07-06 18:36:50 -04:00
*
* ### Example
*
* ```
* @NgModule({
* imports: [RouterModule.forChild(CHILD_ROUTES)]
* })
* class Lazy {}
2016-07-06 18:36:50 -04:00
* ```
*
* @experimental
*/
@NgModule({declarations: ROUTER_DIRECTIVES, exports: ROUTER_DIRECTIVES})
export class RouterModule {
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders {
return {
ngModule: RouterModule,
providers: [
ROUTER_PROVIDERS, provideRoutes(routes),
{provide: ROUTER_CONFIGURATION, useValue: config ? config : {}}, {
provide: LocationStrategy,
useFactory: provideLocationStrategy,
2016-07-28 17:36:05 -04:00
deps: [
PlatformLocation, [new Inject(APP_BASE_HREF), new Optional()], ROUTER_CONFIGURATION
]
},
provideRouterInitializer()
]
};
}
static forChild(routes: Routes): ModuleWithProviders {
return {ngModule: RouterModule, providers: [provideRoutes(routes)]};
}
}
export function provideLocationStrategy(
platformLocationStrategy: PlatformLocation, baseHref: string, options: ExtraOptions = {}) {
return options.useHash ? new HashLocationStrategy(platformLocationStrategy, baseHref) :
new PathLocationStrategy(platformLocationStrategy, baseHref);
}