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