fix(router): reset URL to the stable state when a navigation gets canceled
Closes #10321
This commit is contained in:
parent
8e221b826f
commit
d509ee078b
|
@ -702,7 +702,7 @@ export class Router {
|
||||||
this.currentRouterState = state;
|
this.currentRouterState = state;
|
||||||
|
|
||||||
if (!shouldPreventPushState) {
|
if (!shouldPreventPushState) {
|
||||||
let path = this.urlSerializer.serialize(this.rawUrlTree);
|
const path = this.urlSerializer.serialize(this.rawUrlTree);
|
||||||
if (this.location.isCurrentPathEqualTo(path) || shouldReplaceUrl) {
|
if (this.location.isCurrentPathEqualTo(path) || shouldReplaceUrl) {
|
||||||
this.location.replaceState(path);
|
this.location.replaceState(path);
|
||||||
} else {
|
} else {
|
||||||
|
@ -722,12 +722,14 @@ export class Router {
|
||||||
new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl)));
|
new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl)));
|
||||||
resolvePromise(true);
|
resolvePromise(true);
|
||||||
} else {
|
} else {
|
||||||
|
this.resetUrlToCurrentUrlTree();
|
||||||
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url), ''));
|
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url), ''));
|
||||||
resolvePromise(false);
|
resolvePromise(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(e: any) => {
|
(e: any) => {
|
||||||
if (e instanceof NavigationCancelingError) {
|
if (e instanceof NavigationCancelingError) {
|
||||||
|
this.resetUrlToCurrentUrlTree();
|
||||||
this.navigated = true;
|
this.navigated = true;
|
||||||
this.routerEvents.next(
|
this.routerEvents.next(
|
||||||
new NavigationCancel(id, this.serializeUrl(url), e.message));
|
new NavigationCancel(id, this.serializeUrl(url), e.message));
|
||||||
|
@ -741,14 +743,17 @@ export class Router {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id === this.navigationId) {
|
|
||||||
this.currentRouterState = storedState;
|
this.currentRouterState = storedState;
|
||||||
this.currentUrlTree = storedUrl;
|
this.currentUrlTree = storedUrl;
|
||||||
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
|
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
|
||||||
this.location.replaceState(this.serializeUrl(this.rawUrlTree));
|
this.location.replaceState(this.serializeUrl(this.rawUrlTree));
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
private resetUrlToCurrentUrlTree(): void {
|
||||||
|
const path = this.urlSerializer.serialize(this.rawUrlTree);
|
||||||
|
this.location.replaceState(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1213,6 +1213,35 @@ describe('Integration', () => {
|
||||||
expect(location.path()).toEqual('/team/22');
|
expect(location.path()).toEqual('/team/22');
|
||||||
})));
|
})));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('should reset the location when cancleling a navigation', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [{
|
||||||
|
provide: 'alwaysFalse',
|
||||||
|
useValue: (a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => { return false; }
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
router.resetConfig([
|
||||||
|
{path: 'one', component: SimpleCmp},
|
||||||
|
{path: 'two', component: SimpleCmp, canActivate: ['alwaysFalse']}
|
||||||
|
]);
|
||||||
|
|
||||||
|
router.navigateByUrl('/one');
|
||||||
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/one');
|
||||||
|
|
||||||
|
location.go('/two');
|
||||||
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/one');
|
||||||
|
|
||||||
|
})));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('CanDeactivate', () => {
|
describe('CanDeactivate', () => {
|
||||||
|
|
|
@ -228,7 +228,7 @@ export declare class RouterLink {
|
||||||
routerLink: any[] | string;
|
routerLink: any[] | string;
|
||||||
urlTree: UrlTree;
|
urlTree: UrlTree;
|
||||||
constructor(router: Router, route: ActivatedRoute, locationStrategy: LocationStrategy);
|
constructor(router: Router, route: ActivatedRoute, locationStrategy: LocationStrategy);
|
||||||
onClick(button: number, ctrlKey: boolean, metaKey: boolean): boolean;
|
onClick(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
|
|
Loading…
Reference in New Issue