fix(router): add an option to disable initial navigation
This commit is contained in:
parent
2fc5c57b31
commit
a2deafc50f
|
@ -12,8 +12,8 @@ export {RouterLink, RouterLinkWithHref} from './src/directives/router_link';
|
||||||
export {RouterLinkActive} from './src/directives/router_link_active';
|
export {RouterLinkActive} from './src/directives/router_link_active';
|
||||||
export {RouterOutlet} from './src/directives/router_outlet';
|
export {RouterOutlet} from './src/directives/router_outlet';
|
||||||
export {CanActivate, CanActivateChild, CanDeactivate, CanLoad, Resolve} from './src/interfaces';
|
export {CanActivate, CanActivateChild, CanDeactivate, CanLoad, Resolve} from './src/interfaces';
|
||||||
export {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationExtras, NavigationStart, Router, RoutesRecognized} from './src/router';
|
export {ErrorHandler, Event, NavigationCancel, NavigationEnd, NavigationError, NavigationExtras, NavigationStart, Router, RoutesRecognized} from './src/router';
|
||||||
export {ExtraOptions, RouterModule, provideRoutes} from './src/router_module';
|
export {ExtraOptions, ROUTER_DIRECTIVES, RouterModule, provideRoutes} from './src/router_module';
|
||||||
export {RouterOutletMap} from './src/router_outlet_map';
|
export {RouterOutletMap} from './src/router_outlet_map';
|
||||||
export {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from './src/router_state';
|
export {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from './src/router_state';
|
||||||
export {PRIMARY_OUTLET, Params} from './src/shared';
|
export {PRIMARY_OUTLET, Params} from './src/shared';
|
||||||
|
|
|
@ -262,6 +262,22 @@ export class Router {
|
||||||
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
|
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 = <any>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.
|
* Returns the current route state.
|
||||||
*/
|
*/
|
||||||
|
@ -439,19 +455,6 @@ export class Router {
|
||||||
(_) => this.runNavigate(url, extras.skipLocationChange, extras.replaceUrl, id));
|
(_) => 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 = <any>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(
|
private runNavigate(
|
||||||
url: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean,
|
url: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean,
|
||||||
id: number): Promise<boolean> {
|
id: number): Promise<boolean> {
|
||||||
|
|
|
@ -148,6 +148,7 @@ export function provideRoutes(routes: Routes): any {
|
||||||
export interface ExtraOptions {
|
export interface ExtraOptions {
|
||||||
enableTracing?: boolean;
|
enableTracing?: boolean;
|
||||||
useHash?: boolean;
|
useHash?: boolean;
|
||||||
|
initialNavigation?: boolean;
|
||||||
errorHandler?: ErrorHandler;
|
errorHandler?: ErrorHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,8 +184,14 @@ export function rootRoute(router: Router): ActivatedRoute {
|
||||||
return router.routerState.root;
|
return router.routerState.root;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initialRouterNavigation(router: Router) {
|
export function initialRouterNavigation(router: Router, opts: ExtraOptions) {
|
||||||
return () => { router.initialNavigation(); };
|
return () => {
|
||||||
|
if (opts.initialNavigation === false) {
|
||||||
|
router.setUpLocationChangeListener();
|
||||||
|
} else {
|
||||||
|
router.initialNavigation();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function provideRouterInitializer() {
|
export function provideRouterInitializer() {
|
||||||
|
@ -192,6 +199,6 @@ export function provideRouterInitializer() {
|
||||||
provide: APP_BOOTSTRAP_LISTENER,
|
provide: APP_BOOTSTRAP_LISTENER,
|
||||||
multi: true,
|
multi: true,
|
||||||
useFactory: initialRouterNavigation,
|
useFactory: initialRouterNavigation,
|
||||||
deps: [Router]
|
deps: [Router, ROUTER_CONFIGURATION]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,9 @@ export declare class DefaultUrlSerializer implements UrlSerializer {
|
||||||
serialize(tree: UrlTree): string;
|
serialize(tree: UrlTree): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @stable */
|
||||||
|
export declare type ErrorHandler = (error: any) => any;
|
||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
export declare type Event = NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized;
|
export declare type Event = NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized;
|
||||||
|
|
||||||
|
@ -73,6 +76,7 @@ export declare type Event = NavigationStart | NavigationEnd | NavigationCancel |
|
||||||
export interface ExtraOptions {
|
export interface ExtraOptions {
|
||||||
enableTracing?: boolean;
|
enableTracing?: boolean;
|
||||||
errorHandler?: ErrorHandler;
|
errorHandler?: ErrorHandler;
|
||||||
|
initialNavigation?: boolean;
|
||||||
useHash?: boolean;
|
useHash?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,6 +189,7 @@ export declare class Router {
|
||||||
parseUrl(url: string): UrlTree;
|
parseUrl(url: string): UrlTree;
|
||||||
resetConfig(config: Routes): void;
|
resetConfig(config: Routes): void;
|
||||||
serializeUrl(url: UrlTree): string;
|
serializeUrl(url: UrlTree): string;
|
||||||
|
setUpLocationChangeListener(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
|
|
Loading…
Reference in New Issue