2016-06-23 09:47:54 -07: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-06-08 17:46:02 -07:00
|
|
|
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
|
2016-07-06 11:02:16 -07:00
|
|
|
import {APP_INITIALIZER, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
|
2016-06-08 17:46:02 -07:00
|
|
|
|
|
|
|
|
import {RouterConfig} from './config';
|
|
|
|
|
import {Router} from './router';
|
2016-07-06 11:32:42 -07:00
|
|
|
import {ROUTER_CONFIG} from './router_config_loader';
|
2016-06-08 17:46:02 -07:00
|
|
|
import {RouterOutletMap} from './router_outlet_map';
|
|
|
|
|
import {ActivatedRoute} from './router_state';
|
2016-06-21 11:56:40 -07:00
|
|
|
import {DefaultUrlSerializer, UrlSerializer} from './url_tree';
|
2016-06-08 17:46:02 -07:00
|
|
|
|
2016-06-14 15:20:43 -07:00
|
|
|
export const ROUTER_OPTIONS = new OpaqueToken('ROUTER_OPTIONS');
|
|
|
|
|
|
2016-06-27 12:27:23 -07:00
|
|
|
/**
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
2016-06-09 14:33:09 -07:00
|
|
|
export interface ExtraOptions { enableTracing?: boolean; }
|
2016-06-08 17:46:02 -07:00
|
|
|
|
2016-06-14 15:20:43 -07:00
|
|
|
export function setupRouter(
|
2016-06-15 16:45:19 -07:00
|
|
|
ref: ApplicationRef, resolver: ComponentResolver, urlSerializer: UrlSerializer,
|
2016-07-06 11:02:16 -07:00
|
|
|
outletMap: RouterOutletMap, location: Location, injector: Injector,
|
|
|
|
|
loader: AppModuleFactoryLoader, config: RouterConfig, opts: ExtraOptions) {
|
2016-06-14 15:20:43 -07:00
|
|
|
if (ref.componentTypes.length == 0) {
|
|
|
|
|
throw new Error('Bootstrap at least one component before injecting Router.');
|
|
|
|
|
}
|
|
|
|
|
const componentType = ref.componentTypes[0];
|
2016-07-06 11:02:16 -07:00
|
|
|
const r = new Router(
|
|
|
|
|
componentType, resolver, urlSerializer, outletMap, location, injector, loader, config);
|
2016-06-14 15:20:43 -07:00
|
|
|
ref.registerDisposeListener(() => r.dispose());
|
|
|
|
|
|
|
|
|
|
if (opts.enableTracing) {
|
|
|
|
|
r.events.subscribe(e => {
|
|
|
|
|
console.group(`Router Event: ${(<any>e.constructor).name}`);
|
|
|
|
|
console.log(e.toString());
|
|
|
|
|
console.log(e);
|
|
|
|
|
console.groupEnd();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setupRouterInitializer(injector: Injector) {
|
|
|
|
|
// https://github.com/angular/angular/issues/9101
|
|
|
|
|
// Delay the router instantiation to avoid circular dependency (ApplicationRef ->
|
|
|
|
|
// APP_INITIALIZER -> Router)
|
2016-06-15 16:45:19 -07:00
|
|
|
setTimeout(() => {
|
2016-06-14 15:20:43 -07:00
|
|
|
const appRef = injector.get(ApplicationRef);
|
|
|
|
|
if (appRef.componentTypes.length == 0) {
|
2016-06-15 16:45:19 -07:00
|
|
|
appRef.registerBootstrapListener(() => { injector.get(Router).initialNavigation(); });
|
2016-06-14 15:20:43 -07:00
|
|
|
} else {
|
|
|
|
|
injector.get(Router).initialNavigation();
|
|
|
|
|
}
|
|
|
|
|
}, 0);
|
2016-06-15 16:45:19 -07:00
|
|
|
return (): any => null;
|
2016-06-14 15:20:43 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-08 17:46:02 -07:00
|
|
|
/**
|
2016-06-28 14:49:29 -07:00
|
|
|
* An array of {@link Provider}s. To use the router, you must add this to your application.
|
2016-06-08 17:46:02 -07:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @Component({directives: [ROUTER_DIRECTIVES]})
|
|
|
|
|
* class AppCmp {
|
|
|
|
|
* // ...
|
|
|
|
|
* }
|
|
|
|
|
*
|
2016-06-28 14:49:29 -07:00
|
|
|
* const config = [
|
|
|
|
|
* {path: 'home', component: Home}
|
2016-06-08 17:46:02 -07:00
|
|
|
* ];
|
|
|
|
|
*
|
2016-06-28 14:49:29 -07:00
|
|
|
* bootstrap(AppCmp, [provideRouter(config)]);
|
2016-06-08 17:46:02 -07:00
|
|
|
* ```
|
2016-06-27 12:27:23 -07:00
|
|
|
*
|
2016-07-06 15:36:50 -07:00
|
|
|
* @deprecated use RouterAppModule instead
|
2016-06-08 17:46:02 -07:00
|
|
|
*/
|
2016-06-14 15:20:43 -07:00
|
|
|
export function provideRouter(_config: RouterConfig, _opts: ExtraOptions): any[] {
|
2016-06-08 17:46:02 -07:00
|
|
|
return [
|
2016-06-14 15:20:43 -07:00
|
|
|
{provide: ROUTER_CONFIG, useValue: _config}, {provide: ROUTER_OPTIONS, useValue: _opts},
|
2016-06-09 14:33:09 -07:00
|
|
|
Location, {provide: LocationStrategy, useClass: PathLocationStrategy},
|
2016-06-08 17:46:02 -07:00
|
|
|
{provide: UrlSerializer, useClass: DefaultUrlSerializer},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
provide: Router,
|
2016-06-14 15:20:43 -07:00
|
|
|
useFactory: setupRouter,
|
|
|
|
|
deps: [
|
|
|
|
|
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
|
2016-07-06 11:02:16 -07:00
|
|
|
AppModuleFactoryLoader, ROUTER_CONFIG, ROUTER_OPTIONS
|
2016-06-14 15:20:43 -07:00
|
|
|
]
|
2016-06-08 17:46:02 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
RouterOutletMap,
|
2016-06-15 16:45:19 -07:00
|
|
|
{provide: ActivatedRoute, useFactory: (r: Router) => r.routerState.root, deps: [Router]},
|
2016-06-08 17:46:02 -07:00
|
|
|
|
|
|
|
|
// Trigger initial navigation
|
2016-07-06 11:02:16 -07:00
|
|
|
{provide: APP_INITIALIZER, multi: true, useFactory: setupRouterInitializer, deps: [Injector]},
|
|
|
|
|
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader}
|
2016-06-08 17:46:02 -07:00
|
|
|
];
|
|
|
|
|
}
|
2016-07-06 11:02:16 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Router configuration.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @AppModule({providers: [
|
|
|
|
|
* provideRoutes([{path: 'home', component: Home}])
|
|
|
|
|
* ]})
|
|
|
|
|
* class LazyLoadedModule {
|
|
|
|
|
* // ...
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export function provideRoutes(config: RouterConfig): any {
|
|
|
|
|
return {provide: ROUTER_CONFIG, useValue: config};
|
2016-07-06 15:36:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Router configuration.
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* @AppModule({providers: [
|
|
|
|
|
* provideRouterOptions({enableTracing: true})
|
|
|
|
|
* ]})
|
|
|
|
|
* class LazyLoadedModule {
|
|
|
|
|
* // ...
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export function provideRouterConfig(options: ExtraOptions): any {
|
|
|
|
|
return {provide: ROUTER_OPTIONS, useValue: options};
|
|
|
|
|
}
|