How to update matrix parameters in Angular2
The approach that ended up working well for me is this:
let route: ActivatedRoute;
const newUrl = router.createUrlTree([
merge({'a': 123}, route.snapshot.params)
], {relativeTo: route});
By using merge, you can add, update and subtract url parameters and then use router.navigateByUrl(newUrl)
in order to execute.
add: merge({newParam: 111}, route.snapshot.params)
update: merge({param: 111}, route.snapshot.params)
subtract: merge({param: null}, route.snapshot.params)
Hope others find this as useful as I have.
Another example using Object.assign instead of merge:
let route = this.route; // The imported ActivatedRoute
let currentParamsObj: Params = Object.assign({}, route.params);
let newParam = {};
newParam[key] = value;
let newUrl = this._router.createUrlTree([
Object.assign(currentParamsObj, newParam)
], {relativeTo: this.route });
this._router.navigateByUrl(newUrl);