Add / change optional parameters (matrix designation) in the URL without navigation

I know how to use additional parameters for the router, for example:

this._router.navigate(['/users', { page: 2 }]);

will lead to navigation to /users;page=2.

What I would like to do is to be able to change these parameters without actually navigating. Just change the url to update / add parameters.

This can be useful, for example, when displaying a table, and the user changes the sort key or adds a filter.

+4
source share
1 answer

router.navigate(). , , .

, ActivatedRoute.params Observable:

import {ActivatedRoute} from '@angular/router';
...
constructor(private route:ActivatedRoute){}

ngOnInit() {
  this.sub = this.route.params.subscribe(params => {
     let page = +params['page']; // (+) converts string 'id' to a number
     let sort = params['sort'] || 'ASC'; //use ASC order if none is supplied
     this.updateUI(page,sort);//function would update component as you wish
   });
}

params.subscribe .

+4

Source: https://habr.com/ru/post/1653619/


All Articles