fix(router): add an option to disable initial navigation

This commit is contained in:
vsavkin 2016-08-25 08:48:31 -07:00 committed by Victor Berchet
parent 2fc5c57b31
commit a2deafc50f
4 changed files with 33 additions and 18 deletions

View File

@ -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';

View File

@ -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 = <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.
*/
@ -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 = <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(
url: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean,
id: number): Promise<boolean> {

View File

@ -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]
};
}

View File

@ -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 */