@@ -382,23 +382,25 @@ export class Router {
382382 setUpLocationChangeListener ( ) : void {
383383 // Zone.current.wrap is needed because of the issue with RxJS scheduler,
384384 // which does not work properly with zone.js in IE and Safari
385- this . locationSubscription = < any > this . location . subscribe ( Zone . current . wrap ( ( change : any ) => {
386- const rawUrlTree = this . urlSerializer . parse ( change [ 'url' ] ) ;
387- const lastNavigation = this . navigations . value ;
388-
389- // If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
390- // and that navigation results in 'replaceState' that leads to the same URL,
391- // we should skip those.
392- if ( lastNavigation && lastNavigation . imperative &&
393- lastNavigation . rawUrl . toString ( ) === rawUrlTree . toString ( ) ) {
394- return ;
395- }
385+ if ( ! this . locationSubscription ) {
386+ this . locationSubscription = < any > this . location . subscribe ( Zone . current . wrap ( ( change : any ) => {
387+ const rawUrlTree = this . urlSerializer . parse ( change [ 'url' ] ) ;
388+ const lastNavigation = this . navigations . value ;
389+
390+ // If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
391+ // and that navigation results in 'replaceState' that leads to the same URL,
392+ // we should skip those.
393+ if ( lastNavigation && lastNavigation . imperative &&
394+ lastNavigation . rawUrl . toString ( ) === rawUrlTree . toString ( ) ) {
395+ return ;
396+ }
396397
397- setTimeout ( ( ) => {
398- this . scheduleNavigation (
399- rawUrlTree , false , { skipLocationChange : change [ 'pop' ] , replaceUrl : true } ) ;
400- } , 0 ) ;
401- } ) ) ;
398+ setTimeout ( ( ) => {
399+ this . scheduleNavigation (
400+ rawUrlTree , false , { skipLocationChange : change [ 'pop' ] , replaceUrl : true } ) ;
401+ } , 0 ) ;
402+ } ) ) ;
403+ }
402404 }
403405
404406 /**
@@ -443,7 +445,12 @@ export class Router {
443445 /**
444446 * Disposes of the router.
445447 */
446- dispose ( ) : void { this . locationSubscription . unsubscribe ( ) ; }
448+ dispose ( ) : void {
449+ if ( this . locationSubscription ) {
450+ this . locationSubscription . unsubscribe ( ) ;
451+ this . locationSubscription = null ;
452+ }
453+ }
447454
448455 /**
449456 * Applies an array of commands to the current url tree and creates a new url tree.
0 commit comments