diff --git a/modules/@angular/router/index.ts b/modules/@angular/router/index.ts index d3565679fc..1cee5c9299 100644 --- a/modules/@angular/router/index.ts +++ b/modules/@angular/router/index.ts @@ -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]; diff --git a/modules/@angular/router/src/common_router_providers.ts b/modules/@angular/router/src/common_router_providers.ts index fa45dfa555..47262b746c 100644 --- a/modules/@angular/router/src/common_router_providers.ts +++ b/modules/@angular/router/src/common_router_providers.ts @@ -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}; -} \ No newline at end of file +} + +/** + * Router configuration. + * + * ### Example + * + * ``` + * @AppModule({providers: [ + * provideRouterOptions({enableTracing: true}) + * ]}) + * class LazyLoadedModule { + * // ... + * } + * ``` + * + * @experimental + */ +export function provideRouterConfig(options: ExtraOptions): any { + return {provide: ROUTER_OPTIONS, useValue: options}; +} diff --git a/modules/@angular/router/src/router_app_module.ts b/modules/@angular/router/src/router_app_module.ts new file mode 100644 index 0000000000..36b87eea56 --- /dev/null +++ b/modules/@angular/router/src/router_app_module.ts @@ -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); + } +} \ No newline at end of file diff --git a/modules/playground/src/routing/index.ts b/modules/playground/src/routing/index.ts index 19dce5c943..c9e8682189 100644 --- a/modules/playground/src/routing/index.ts +++ b/modules/playground/src/routing/index.ts @@ -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] + }); +} \ No newline at end of file