fix(router): navigation should not preserve query params and fragment by default
BREAKING CHANGE Previously both imperative (router.navigate) and declarative (routerLink) navigations would preserve the current query params and fragment. This behavior turned out to be confusing. This commit changes it. Now, neither is preserved by default. To preserve them, you need to do the following: router.naviage("newUrl", {preserveQueryParams: true, preserveFragment: true}) <a routerLink="newUrl" preserveQueryParams preserveFragment></a>
This commit is contained in:
@ -51,9 +51,15 @@ import {UrlTree} from '../url_tree';
|
||||
* <a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">link to user
|
||||
component</a>
|
||||
* ```
|
||||
*
|
||||
* RouterLink will use these to generate this link: `/user/bob#education?debug=true`.
|
||||
*
|
||||
* You can also tell the directive to preserve the current query params and fragment:
|
||||
*
|
||||
* ```
|
||||
* <a [routerLink]="['/user/bob']" preserveQueryParams preserveFragment>link to user
|
||||
component</a>
|
||||
* ```
|
||||
*
|
||||
* @stable
|
||||
*/
|
||||
@Directive({selector: ':not(a)[routerLink]'})
|
||||
@ -61,6 +67,8 @@ export class RouterLink {
|
||||
private commands: any[] = [];
|
||||
@Input() queryParams: {[k: string]: any};
|
||||
@Input() fragment: string;
|
||||
@Input() preserveQueryParams: boolean;
|
||||
@Input() preserveFragment: boolean;
|
||||
|
||||
constructor(
|
||||
private router: Router, private route: ActivatedRoute,
|
||||
@ -85,9 +93,13 @@ export class RouterLink {
|
||||
}
|
||||
|
||||
get urlTree(): UrlTree {
|
||||
return this.router.createUrlTree(
|
||||
this.commands,
|
||||
{relativeTo: this.route, queryParams: this.queryParams, fragment: this.fragment});
|
||||
return this.router.createUrlTree(this.commands, {
|
||||
relativeTo: this.route,
|
||||
queryParams: this.queryParams,
|
||||
fragment: this.fragment,
|
||||
preserveQueryParams: toBool(this.preserveQueryParams),
|
||||
preserveFragment: toBool(this.preserveFragment)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,6 +113,9 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
|
||||
private commands: any[] = [];
|
||||
@Input() queryParams: {[k: string]: any};
|
||||
@Input() fragment: string;
|
||||
@Input() routerLinkOptions: {preserveQueryParams: boolean, preserveFragment: boolean};
|
||||
@Input() preserveQueryParams: boolean;
|
||||
@Input() preserveFragment: boolean;
|
||||
private subscription: Subscription;
|
||||
|
||||
// the url displayed on the anchor element.
|
||||
@ -148,12 +163,21 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
|
||||
}
|
||||
|
||||
private updateTargetUrlAndHref(): void {
|
||||
this.urlTree = this.router.createUrlTree(
|
||||
this.commands,
|
||||
{relativeTo: this.route, queryParams: this.queryParams, fragment: this.fragment});
|
||||
this.urlTree = this.router.createUrlTree(this.commands, {
|
||||
relativeTo: this.route,
|
||||
queryParams: this.queryParams,
|
||||
fragment: this.fragment,
|
||||
preserveQueryParams: toBool(this.preserveQueryParams),
|
||||
preserveFragment: toBool(this.preserveFragment)
|
||||
});
|
||||
|
||||
if (this.urlTree) {
|
||||
this.href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.urlTree));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function toBool(s?: any): boolean {
|
||||
if (s === '') return true;
|
||||
return !!s;
|
||||
}
|
Reference in New Issue
Block a user