angular-cn/modules/@angular/router/src/router_module.ts
Tobias Bosch 46b212706b refactor(core): change module semantics
This contains major changes to the compiler, bootstrap of the platforms
and test environment initialization.

Main part of #10043
Closes #10164

BREAKING CHANGE:
- Semantics and name of `@AppModule` (now `@NgModule`) changed quite a bit.
  This is actually not breaking as `@AppModules` were not part of rc.4.
  We will have detailed docs on `@NgModule` separately.
- `coreLoadAndBootstrap` and `coreBootstrap` can't be used any more (without migration support).
  Use `bootstrapModule` / `bootstrapModuleFactory` instead.
- All Components listed in routes have to be part of the `declarations` of an NgModule.
  Either directly on the bootstrap module / lazy loaded module, or in an NgModule imported by them.
2016-07-26 07:04:10 -07:00

89 lines
2.7 KiB
TypeScript

/**
* @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';
import {ApplicationRef, ComponentResolver, Injector, NgModule, NgModuleFactoryLoader, OpaqueToken, SystemJsNgModuleLoader} from '@angular/core';
import {ROUTER_CONFIGURATION, rootRoute, 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';
import {Router} from './router';
import {ROUTES} from './router_config_loader';
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];
export const ROUTER_PROVIDERS: any[] = [
Location, {provide: LocationStrategy, useClass: PathLocationStrategy},
{provide: UrlSerializer, useClass: DefaultUrlSerializer}, {
provide: Router,
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
NgModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
]
},
RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
{provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader},
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
];
/**
* Router module to be used for lazy loaded parts.
*
* ### Example
*
* ```
* @NgModule({
* imports: [RouterModuleWithoutProviders]
* })
* class TeamsModule {}
* ```
*
* @experimental We will soon have a way for the `RouterModule` to be imported with and without a
* provider,
* and then this module will be removed.
*/
@NgModule({declarations: ROUTER_DIRECTIVES, exports: ROUTER_DIRECTIVES})
export class RouterModuleWithoutProviders {
}
/**
* Router module.
*
* ### Example
*
* ```
* bootstrap(AppCmp, {modules: [RouterModule]});
* ```
*
* @experimental
*/
@NgModule({exports: [RouterModuleWithoutProviders], providers: ROUTER_PROVIDERS})
export class RouterModule {
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);
}
}