fix(providers): make providers static analysis friendly
This commit is contained in:
parent
127401598b
commit
15f27b5455
|
@ -1,5 +1,5 @@
|
||||||
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
|
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
|
||||||
import {APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector} from '@angular/core';
|
import {APP_INITIALIZER, ApplicationRef, ComponentResolver, Injector, OpaqueToken} from '@angular/core';
|
||||||
|
|
||||||
import {RouterConfig} from './config';
|
import {RouterConfig} from './config';
|
||||||
import {Router} from './router';
|
import {Router} from './router';
|
||||||
|
@ -7,8 +7,48 @@ import {RouterOutletMap} from './router_outlet_map';
|
||||||
import {ActivatedRoute} from './router_state';
|
import {ActivatedRoute} from './router_state';
|
||||||
import {DefaultUrlSerializer, UrlSerializer} from './url_serializer';
|
import {DefaultUrlSerializer, UrlSerializer} from './url_serializer';
|
||||||
|
|
||||||
|
export const ROUTER_CONFIG = new OpaqueToken('ROUTER_CONFIG');
|
||||||
|
export const ROUTER_OPTIONS = new OpaqueToken('ROUTER_OPTIONS');
|
||||||
|
|
||||||
export interface ExtraOptions { enableTracing?: boolean; }
|
export interface ExtraOptions { enableTracing?: boolean; }
|
||||||
|
|
||||||
|
export function setupRouter(
|
||||||
|
ref, resolver, urlSerializer, outletMap, location, injector, config, opts) {
|
||||||
|
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, injector, config);
|
||||||
|
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)
|
||||||
|
setTimeout(_ => {
|
||||||
|
const appRef = injector.get(ApplicationRef);
|
||||||
|
if (appRef.componentTypes.length == 0) {
|
||||||
|
appRef.registerBootstrapListener((_) => { injector.get(Router).initialNavigation(); });
|
||||||
|
} else {
|
||||||
|
injector.get(Router).initialNavigation();
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
return _ => null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of {@link Provider}s. To use the router, you must add this to your application.
|
* A list of {@link Provider}s. To use the router, you must add this to your application.
|
||||||
*
|
*
|
||||||
|
@ -27,59 +67,25 @@ export interface ExtraOptions { enableTracing?: boolean; }
|
||||||
* bootstrap(AppCmp, [provideRouter(router)]);
|
* bootstrap(AppCmp, [provideRouter(router)]);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function provideRouter(config: RouterConfig, opts: ExtraOptions): any[] {
|
export function provideRouter(_config: RouterConfig, _opts: ExtraOptions): any[] {
|
||||||
return [
|
return [
|
||||||
|
{provide: ROUTER_CONFIG, useValue: _config}, {provide: ROUTER_OPTIONS, useValue: _opts},
|
||||||
Location, {provide: LocationStrategy, useClass: PathLocationStrategy},
|
Location, {provide: LocationStrategy, useClass: PathLocationStrategy},
|
||||||
{provide: UrlSerializer, useClass: DefaultUrlSerializer},
|
{provide: UrlSerializer, useClass: DefaultUrlSerializer},
|
||||||
|
|
||||||
{
|
{
|
||||||
provide: Router,
|
provide: Router,
|
||||||
useFactory: (ref, resolver, urlSerializer, outletMap, location, injector) => {
|
useFactory: setupRouter,
|
||||||
if (ref.componentTypes.length == 0) {
|
deps: [
|
||||||
throw new Error('Bootstrap at least one component before injecting Router.');
|
ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector,
|
||||||
}
|
ROUTER_CONFIG, ROUTER_OPTIONS
|
||||||
const componentType = ref.componentTypes[0];
|
]
|
||||||
const r = new Router(
|
|
||||||
componentType, resolver, urlSerializer, outletMap, location, injector, config);
|
|
||||||
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;
|
|
||||||
},
|
|
||||||
deps:
|
|
||||||
[ApplicationRef, ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector]
|
|
||||||
},
|
},
|
||||||
|
|
||||||
RouterOutletMap,
|
RouterOutletMap,
|
||||||
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]},
|
{provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]},
|
||||||
|
|
||||||
// Trigger initial navigation
|
// Trigger initial navigation
|
||||||
{
|
{provide: APP_INITIALIZER, multi: true, useFactory: setupRouterInitializer, deps: [Injector]}
|
||||||
provide: APP_INITIALIZER,
|
|
||||||
multi: true,
|
|
||||||
useFactory: (injector) => {
|
|
||||||
// https://github.com/angular/angular/issues/9101
|
|
||||||
// Delay the router instantiation to avoid circular dependency (ApplicationRef ->
|
|
||||||
// APP_INITIALIZER -> Router)
|
|
||||||
setTimeout(_ => {
|
|
||||||
const appRef = injector.get(ApplicationRef);
|
|
||||||
if (appRef.componentTypes.length == 0) {
|
|
||||||
appRef.registerBootstrapListener((_) => { injector.get(Router).initialNavigation(); });
|
|
||||||
} else {
|
|
||||||
injector.get(Router).initialNavigation();
|
|
||||||
}
|
|
||||||
}, 0);
|
|
||||||
return _ => null;
|
|
||||||
},
|
|
||||||
deps: [Injector]
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue