diff --git a/packages/common/src/location/location.ts b/packages/common/src/location/location.ts index fbf0675cc8..55af1e738d 100644 --- a/packages/common/src/location/location.ts +++ b/packages/common/src/location/location.ts @@ -64,7 +64,8 @@ export class Location { _platformLocation: PlatformLocation; /** @internal */ _urlChangeListeners: ((url: string, state: unknown) => void)[] = []; - private _urlChangeSubscription?: SubscriptionLike; + /** @internal */ + _urlChangeSubscription?: SubscriptionLike; constructor(platformStrategy: LocationStrategy, platformLocation: PlatformLocation) { this._platformStrategy = platformStrategy; diff --git a/packages/common/testing/src/location_mock.ts b/packages/common/testing/src/location_mock.ts index 28f57c26a5..e7f5595ec9 100644 --- a/packages/common/testing/src/location_mock.ts +++ b/packages/common/testing/src/location_mock.ts @@ -30,6 +30,8 @@ export class SpyLocation implements Location { _platformLocation: PlatformLocation = null!; /** @internal */ _urlChangeListeners: ((url: string, state: unknown) => void)[] = []; + /** @internal */ + _urlChangeSubscription?: SubscriptionLike; setInitialPath(url: string) { this._history[this._historyIndex].path = url; @@ -123,9 +125,12 @@ export class SpyLocation implements Location { } onUrlChange(fn: (url: string, state: unknown) => void) { this._urlChangeListeners.push(fn); - this.subscribe(v => { - this._notifyUrlChangeListeners(v.url, v.state); - }); + + if (!this._urlChangeSubscription) { + this._urlChangeSubscription = this.subscribe(v => { + this._notifyUrlChangeListeners(v.url, v.state); + }); + } } /** @internal */