diff --git a/modules/@angular/router/index.ts b/modules/@angular/router/index.ts index cefc5afd4e..c33222672f 100644 --- a/modules/@angular/router/index.ts +++ b/modules/@angular/router/index.ts @@ -12,8 +12,8 @@ export {RouterLink, RouterLinkWithHref} from './src/directives/router_link'; export {RouterLinkActive} from './src/directives/router_link_active'; export {RouterOutlet} from './src/directives/router_outlet'; export {CanActivate, CanActivateChild, CanDeactivate, CanLoad, Resolve} from './src/interfaces'; -export {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationExtras, NavigationStart, Router, RoutesRecognized} from './src/router'; -export {ExtraOptions, RouterModule, provideRoutes} from './src/router_module'; +export {ErrorHandler, Event, NavigationCancel, NavigationEnd, NavigationError, NavigationExtras, NavigationStart, Router, RoutesRecognized} from './src/router'; +export {ExtraOptions, ROUTER_DIRECTIVES, RouterModule, provideRoutes} from './src/router_module'; export {RouterOutletMap} from './src/router_outlet_map'; export {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from './src/router_state'; export {PRIMARY_OUTLET, Params} from './src/shared'; diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index 4c5f7b4984..bba3795e29 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -262,6 +262,22 @@ export class Router { this.navigateByUrl(this.location.path(true), {replaceUrl: true}); } + /** + * Sets up the location change listener + */ + setUpLocationChangeListener(): void { + // Zone.current.wrap is needed because of the issue with RxJS scheduler, + // which does not work properly with zone.js in IE and Safari + this.locationSubscription = this.location.subscribe(Zone.current.wrap((change: any) => { + const tree = this.urlSerializer.parse(change['url']); + // we fire multiple events for a single URL change + // we should navigate only once + return this.currentUrlTree.toString() !== tree.toString() ? + this.scheduleNavigation(tree, {skipLocationChange: change['pop'], replaceUrl: true}) : + null; + })); + } + /** * Returns the current route state. */ @@ -439,19 +455,6 @@ export class Router { (_) => this.runNavigate(url, extras.skipLocationChange, extras.replaceUrl, id)); } - private setUpLocationChangeListener(): void { - // Zone.current.wrap is needed because of the issue with RxJS scheduler, - // which does not work properly with zone.js in IE and Safari - this.locationSubscription = this.location.subscribe(Zone.current.wrap((change: any) => { - const tree = this.urlSerializer.parse(change['url']); - // we fire multiple events for a single URL change - // we should navigate only once - return this.currentUrlTree.toString() !== tree.toString() ? - this.scheduleNavigation(tree, {skipLocationChange: change['pop'], replaceUrl: true}) : - null; - })); - } - private runNavigate( url: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean, id: number): Promise { diff --git a/modules/@angular/router/src/router_module.ts b/modules/@angular/router/src/router_module.ts index c2ee5295bd..d21db85ff8 100644 --- a/modules/@angular/router/src/router_module.ts +++ b/modules/@angular/router/src/router_module.ts @@ -148,6 +148,7 @@ export function provideRoutes(routes: Routes): any { export interface ExtraOptions { enableTracing?: boolean; useHash?: boolean; + initialNavigation?: boolean; errorHandler?: ErrorHandler; } @@ -183,8 +184,14 @@ export function rootRoute(router: Router): ActivatedRoute { return router.routerState.root; } -export function initialRouterNavigation(router: Router) { - return () => { router.initialNavigation(); }; +export function initialRouterNavigation(router: Router, opts: ExtraOptions) { + return () => { + if (opts.initialNavigation === false) { + router.setUpLocationChangeListener(); + } else { + router.initialNavigation(); + } + }; } export function provideRouterInitializer() { @@ -192,6 +199,6 @@ export function provideRouterInitializer() { provide: APP_BOOTSTRAP_LISTENER, multi: true, useFactory: initialRouterNavigation, - deps: [Router] + deps: [Router, ROUTER_CONFIGURATION] }; } diff --git a/tools/public_api_guard/router/index.d.ts b/tools/public_api_guard/router/index.d.ts index e11cdd90b3..0356fbd927 100644 --- a/tools/public_api_guard/router/index.d.ts +++ b/tools/public_api_guard/router/index.d.ts @@ -66,6 +66,9 @@ export declare class DefaultUrlSerializer implements UrlSerializer { serialize(tree: UrlTree): string; } +/** @stable */ +export declare type ErrorHandler = (error: any) => any; + /** @stable */ export declare type Event = NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized; @@ -73,6 +76,7 @@ export declare type Event = NavigationStart | NavigationEnd | NavigationCancel | export interface ExtraOptions { enableTracing?: boolean; errorHandler?: ErrorHandler; + initialNavigation?: boolean; useHash?: boolean; } @@ -185,6 +189,7 @@ export declare class Router { parseUrl(url: string): UrlTree; resetConfig(config: Routes): void; serializeUrl(url: UrlTree): string; + setUpLocationChangeListener(): void; } /** @stable */