diff --git a/modules/@angular/router/src/common_router_providers.ts b/modules/@angular/router/src/common_router_providers.ts new file mode 100644 index 0000000000..04db250b04 --- /dev/null +++ b/modules/@angular/router/src/common_router_providers.ts @@ -0,0 +1,51 @@ +import { RouterOutletMap } from './router_outlet_map'; +import { UrlSerializer, DefaultUrlSerializer } from './url_serializer'; +import { ActivatedRoute } from './router_state'; +import { Router } from './router'; +import { RouterConfig } from './config'; +import { ComponentResolver, ApplicationRef} from '@angular/core'; +import { LocationStrategy, PathLocationStrategy, Location } from '@angular/common'; + +/** + * A list of {@link Provider}s. To use the router, you must add this to your application. + * + * ### Example + * + * ``` + * @Component({directives: [ROUTER_DIRECTIVES]}) + * class AppCmp { + * // ... + * } + * + * const router = [ + * {path: '/home', component: Home} + * ]; + * + * bootstrap(AppCmp, [provideRouter(router)]); + * ``` + */ +export function provideRouter(config: RouterConfig):any[] { + return [ + Location, + {provide: LocationStrategy, useClass: PathLocationStrategy}, + {provide: UrlSerializer, useClass: DefaultUrlSerializer}, + + { + provide: Router, + useFactory: (ref, resolver, urlSerializer, outletMap, location) => { + if (ref.componentTypes.length == 0) { + throw new Error("Bootstrap at least one component before injecting Router."); + } + const componentType = ref.componentTypes[0]; + const r = new Router(componentType, resolver, urlSerializer, outletMap, location); + r.resetConfig(config); + ref.registerDisposeListener(() => r.dispose()); + return r; + }, + deps: [ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location] + }, + + RouterOutletMap, + {provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]}, + ]; +} diff --git a/modules/@angular/router/src/router_providers.ts b/modules/@angular/router/src/router_providers.ts new file mode 100644 index 0000000000..ca6fecd657 --- /dev/null +++ b/modules/@angular/router/src/router_providers.ts @@ -0,0 +1,29 @@ +import { RouterConfig } from './config'; +import * as common from './common_router_providers'; +import {BrowserPlatformLocation} from '@angular/platform-browser'; +import {PlatformLocation} from '@angular/common'; + +/** + * A list of {@link Provider}s. To use the router, you must add this to your application. + * + * ### Example + * + * ``` + * @Component({directives: [ROUTER_DIRECTIVES]}) + * class AppCmp { + * // ... + * } + * + * const router = [ + * {path: '/home', component: Home} + * ]; + * + * bootstrap(AppCmp, [provideRouter(router)]); + * ``` + */ +export function provideRouter(config: RouterConfig):any[] { + return [ + {provide: PlatformLocation, useClass: BrowserPlatformLocation}, + ...common.provideRouter(config) + ]; +}