fix(router): make router provides work with cli and offline compilation
This commit is contained in:
parent
c87847974a
commit
d15a1d64e1
@ -47,6 +47,10 @@ export function setupRouter(
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function rootRoute(router: Router): ActivatedRoute {
|
||||||
|
return router.routerState.root;
|
||||||
|
}
|
||||||
|
|
||||||
export function setupRouterInitializer(injector: Injector) {
|
export function setupRouterInitializer(injector: Injector) {
|
||||||
// https://github.com/angular/angular/issues/9101
|
// https://github.com/angular/angular/issues/9101
|
||||||
// Delay the router instantiation to avoid circular dependency (ApplicationRef ->
|
// Delay the router instantiation to avoid circular dependency (ApplicationRef ->
|
||||||
@ -100,8 +104,7 @@ export function provideRouter(routes: Routes, config: ExtraOptions): any[] {
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
RouterOutletMap,
|
RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
|
||||||
{provide: ActivatedRoute, useFactory: (r: Router) => 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: setupRouterInitializer, deps: [Injector]},
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
|
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
|
||||||
import {AppModule, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
|
import {AppModule, AppModuleFactoryLoader, ApplicationRef, ComponentResolver, Injector, OpaqueToken, SystemJsAppModuleLoader} from '@angular/core';
|
||||||
|
|
||||||
import {ROUTER_CONFIGURATION, setupRouter} from './common_router_providers';
|
import {ROUTER_CONFIGURATION, rootRoute, setupRouter} from './common_router_providers';
|
||||||
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
|
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
|
||||||
import {RouterLinkActive} from './directives/router_link_active';
|
import {RouterLinkActive} from './directives/router_link_active';
|
||||||
import {RouterOutlet} from './directives/router_outlet';
|
import {RouterOutlet} from './directives/router_outlet';
|
||||||
@ -20,6 +20,7 @@ import {ActivatedRoute} from './router_state';
|
|||||||
import {DefaultUrlSerializer, UrlSerializer} from './url_tree';
|
import {DefaultUrlSerializer, UrlSerializer} from './url_tree';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @stable
|
* @stable
|
||||||
*/
|
*/
|
||||||
@ -35,8 +36,7 @@ export const ROUTER_PROVIDERS: any[] = [
|
|||||||
AppModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
|
AppModuleFactoryLoader, ROUTES, ROUTER_CONFIGURATION
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
RouterOutletMap,
|
RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
|
||||||
{provide: ActivatedRoute, useFactory: (r: Router) => r.routerState.root, deps: [Router]},
|
|
||||||
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader},
|
{provide: AppModuleFactoryLoader, useClass: SystemJsAppModuleLoader},
|
||||||
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
|
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
|
||||||
];
|
];
|
||||||
|
@ -37,6 +37,12 @@ export class SpyAppModuleFactoryLoader implements AppModuleFactoryLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupTestingRouter(
|
||||||
|
resolver: ComponentResolver, urlSerializer: UrlSerializer, outletMap: RouterOutletMap,
|
||||||
|
location: Location, loader: AppModuleFactoryLoader, injector: Injector, routes: Routes) {
|
||||||
|
return new Router(null, resolver, urlSerializer, outletMap, location, injector, loader, routes);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A module setting up the router that should be used for testing.
|
* A module setting up the router that should be used for testing.
|
||||||
* It provides spy implementations of Location, LocationStrategy, and AppModuleFactoryLoader.
|
* It provides spy implementations of Location, LocationStrategy, and AppModuleFactoryLoader.
|
||||||
@ -64,12 +70,7 @@ export class SpyAppModuleFactoryLoader implements AppModuleFactoryLoader {
|
|||||||
{provide: AppModuleFactoryLoader, useClass: SpyAppModuleFactoryLoader},
|
{provide: AppModuleFactoryLoader, useClass: SpyAppModuleFactoryLoader},
|
||||||
{
|
{
|
||||||
provide: Router,
|
provide: Router,
|
||||||
useFactory: (resolver: ComponentResolver, urlSerializer: UrlSerializer,
|
useFactory: setupTestingRouter,
|
||||||
outletMap: RouterOutletMap, location: Location, loader: AppModuleFactoryLoader,
|
|
||||||
injector: Injector, routes: Routes) => {
|
|
||||||
return new Router(
|
|
||||||
null, resolver, urlSerializer, outletMap, location, injector, loader, routes);
|
|
||||||
},
|
|
||||||
deps: [
|
deps: [
|
||||||
ComponentResolver, UrlSerializer, RouterOutletMap, Location, AppModuleFactoryLoader,
|
ComponentResolver, UrlSerializer, RouterOutletMap, Location, AppModuleFactoryLoader,
|
||||||
Injector, ROUTES
|
Injector, ROUTES
|
||||||
|
Loading…
x
Reference in New Issue
Block a user