feat(router): add RouterAppModule

This commit is contained in:
vsavkin 2016-07-06 15:36:50 -07:00
parent 6bfd514caf
commit 8aa2a0c1b2
4 changed files with 104 additions and 19 deletions

View File

@ -6,11 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {RouterLink, RouterLinkWithHref} from './src/directives/router_link';
import {RouterLinkActive} from './src/directives/router_link_active';
import {RouterOutlet} from './src/directives/router_outlet';
export {ExtraOptions, provideRoutes} from './src/common_router_providers';
export {ExtraOptions, provideRoutes, provideRouterConfig} from './src/common_router_providers';
export {Data, ResolveData, Route, RouterConfig} from './src/config';
export {RouterLink, RouterLinkWithHref} from './src/directives/router_link';
export {RouterLinkActive} from './src/directives/router_link_active';
@ -21,11 +18,5 @@ export {RouterOutletMap} from './src/router_outlet_map';
export {provideRouter} from './src/router_providers';
export {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from './src/router_state';
export {PRIMARY_OUTLET, Params} from './src/shared';
export {RouterAppModule, ROUTER_DIRECTIVES} from './src/router_app_module';
export {DefaultUrlSerializer, UrlPathWithParams, UrlSerializer, UrlTree} from './src/url_tree';
/**
* @stable
*/
export const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkWithHref, RouterLinkActive];

View File

@ -80,7 +80,7 @@ export function setupRouterInitializer(injector: Injector) {
* bootstrap(AppCmp, [provideRouter(config)]);
* ```
*
* @stable
* @deprecated use RouterAppModule instead
*/
export function provideRouter(_config: RouterConfig, _opts: ExtraOptions): any[] {
return [
@ -124,4 +124,24 @@ export function provideRouter(_config: RouterConfig, _opts: ExtraOptions): any[]
*/
export function provideRoutes(config: RouterConfig): any {
return {provide: ROUTER_CONFIG, useValue: config};
}
}
/**
* Router configuration.
*
* ### Example
*
* ```
* @AppModule({providers: [
* provideRouterOptions({enableTracing: true})
* ]})
* class LazyLoadedModule {
* // ...
* }
* ```
*
* @experimental
*/
export function provideRouterConfig(options: ExtraOptions): any {
return {provide: ROUTER_OPTIONS, useValue: options};
}

View File

@ -0,0 +1,71 @@
/**
* @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 {AppModule} from '@angular/core';
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {APP_INITIALIZER, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
import {Router} from './router';
import {setupRouter, ROUTER_OPTIONS} from './common_router_providers';
import {ROUTER_CONFIG} from './router_config_loader';
import {RouterOutletMap} from './router_outlet_map';
import {ActivatedRoute} from './router_state';
import {DefaultUrlSerializer, UrlSerializer} from './url_tree';
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
import {RouterLinkActive} from './directives/router_link_active';
import {RouterOutlet} from './directives/router_outlet';
/**
* @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},
{provide: UrlSerializer, useClass: DefaultUrlSerializer},
{
provide: Router,
useFactory: setupRouter,
deps: [
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
AppModuleFactoryLoader, ROUTER_CONFIG, ROUTER_OPTIONS
]
},
RouterOutletMap,
{provide: ActivatedRoute, useFactory: (r: Router) => r.routerState.root, deps: [Router]},
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader},
{provide: ROUTER_OPTIONS, useValue: {enableTracing: false}}
]
})
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);
}
}

View File

@ -9,11 +9,14 @@
import {InboxApp, ROUTER_CONFIG} from './app/inbox-app';
import {bootstrap} from '@angular/platform-browser-dynamic';
import {HashLocationStrategy, LocationStrategy} from '@angular/common';
import {provideRouter} from '@angular/router';
import {provideRoutes, RouterAppModule} from '@angular/router';
export function main() {
bootstrap(InboxApp, [
provideRouter(ROUTER_CONFIG),
{provide: LocationStrategy, useClass: HashLocationStrategy}
]);
}
bootstrap(InboxApp, {
providers: [
provideRoutes(ROUTER_CONFIG),
{provide: LocationStrategy, useClass: HashLocationStrategy}
],
modules: [RouterAppModule]
});
}