In Angular 4+, preserveQueryParams
have been deprecated in favor of queryParamsHandling
. The options are either 'merge'
or 'preserve'
.
In-code example (used in NavigationExtras
):
this.router.navigate(['somewhere'], { queryParamsHandling: "preserve" });
In-template example:
<a [routerLink]="['somewhere']" queryParamsHandling="merge">
It just works this way unfortunately:
const q = preserveQueryParams ? this.currentUrlTree.queryParams : queryParams;
You could try to add custom directive like this:
@Directive({selector: 'a[routerLink][merge]'})
export class RouterLinkDirective implements OnInit
{
@Input()
queryParams: {[k: string]: any};
@Input()
preserveQueryParams: boolean;
constructor(private link:RouterLinkWithHref, private route: ActivatedRoute )
{
}
public ngOnInit():void
{
this.link.queryParams = Object.assign(Object.assign({}, this.route.snapshot.queryParams), this.link.queryParams);
console.debug(this.link.queryParams);
}
}
<a [routerLink]="['/cars']" merge [queryParams]="{model: 'renault'}">Click</a>
Update: See DarkNeuron answer below.
There is an open issue and also already a pull request to make preserveQueryParams
a router setting instead of a per navigation setting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With