How would I remove a query parameter from the URL? For example from to
EDIT: This is my version:
this.activatedRoute.queryParams.subscribe(c => {
const params = Object.assign({}, c);
delete params.dapp;
this.router.navigate([], { relativeTo: this.activatedRoute, queryParams: params });
}).unsubscribe();
8 Answers
You can remove a query parameter by using the merge option of queryParamsHandling and passing in null for any params you wish to remove.
// Remove query params
this.router.navigate([], {
queryParams: {
'yourParamName': null,
'youCanRemoveMultiple': null,
},
queryParamsHandling: 'merge'
})
This option is simpler and requires less work to ensure you are not removing other params. You also do not need to worry about cleaning up an observable subscription when your component is destroyed.
UPDATE: @epelc's answer below is the up-to-date and correct way to do this: .
Unfortunately, there is no clear-cut way to do this currently: . However, as jasonaden commented on the linked thread,
This could be done manually by merging the old and new query params, removing the key you don't want.
Here is one way to do that:
Let's say you have your queryParams stored in some properties.
class MyComponent() {
id: string;
pos: string;
sd: string;
constructor(private route: ActivatedRoute, private router: Router) {}
ngOnInit() {
this.route.url.subscribe(next => {
const paramMap = next.queryParamMap;
this.id = paramMap.get('id');
this.pos = paramMap.get('pos');
this.sd = paramMap.get('sd');
});
}
}
A method to clear the pos parameter would look like this:
clearPosParam() {
this.router.navigate(
['.'],
{ relativeTo: this.route, queryParams: { id: this.id, sd: this.sd } }
);
}
This will effectively navigate to the current route and clear your pos query parameter, keeping your other query parameters the same.
This is the best sulotion that i found, you can change url parameters
in constructor use
private _ActivatedRoute: ActivatedRoute
then use this in init or in constructor body
var snapshot = this._ActivatedRoute.snapshot;
const params = { ...snapshot.queryParams };
delete params.pos
this.router.navigate([], { queryParams: params });
Removing Query Params:
import { Router, ActivatedRoute, Params } from '@angular/router';
constructor(private router: Router, private activatedRoute: ActivatedRoute){
}
setQueryParams(){
const qParams: Params = {};
this.router.navigate([], {
relativeTo: this.activatedRoute,
queryParams: qParams,
queryParamsHandling: ''
});
}
You can use native javascript operation to remove queryParams from url and navigate to View using navigateByUrl method
this.route.queryParams
.subscribe((params: Params) => {
if (params && Object.keys(params).length > 0) {
const urlWithoutQueryParams = this.router.url.substring(0, this.router.url.indexOf('?'));
this.router.navigateByUrl(urlWithoutQueryParams)
.then(() => {
// any other functionality when navigation succeeds
params = null;
});
}
});
I've written a few Router Prototype overrides that makes things easier to handle query params:
The idea is to call a method on the router to easily manage routing via parameters, instead of having to export functions/redeclare the functionality every time.
Create an index.d.ts file that contains the prototype override definitions:
// Ensure this is treated as a module.
export { };
declare module '@angular/router' {
interface Router {
updateQueryParams(activatedRoute: ActivatedRoute, params: Params): Promise<boolean>;
setQueryParams(activatedRoute: ActivatedRoute, params: Params): Promise<boolean>;
removeQueryParams(activatedRoute: ActivatedRoute, ...keys: string[]): Promise<boolean>;
}
}
Important:
Make sure you import this file before using this prototype override, I just added my prototype import(s) to the
app.module.ts:
import './shared/prototype-overrides/router.prototypes';
Setting Query Parameters
This will only set the query parameters specified, and not merge the parameters.
Scenario
You are on the following route:
and you want to SET the query parameters to param3=HelloWorld, removing the others.
Usage
this.router.setQueryParams(this.activatedRoute, { param3: 'HelloWorld' });
// Will route to
Prototype function implementation
Router.prototype.setQueryParams = function (activatedRoute: ActivatedRoute, params: Params): Promise<boolean> {
const context: Router = this;
if (isNullOrUndefined(activatedRoute)) {
throw new Error('Cannot update the query parameters - Activated Route not provided to use relative route');
}
return new Promise<boolean>((resolve) => {
setTimeout(() => {
resolve(context.navigate([], {
relativeTo: activatedRoute,
queryParams: params
}));
});
});
};