I have set up the following routing system
export const MyRoutes: Routes = [
{path: \'\', redirectTo
Use ActivatedRouteSnapshot from your ActivatedRoute
ActivatedRouteSnapshot
interface has params
and queryParams
property, and you could get the both value at the same time.
constructor(private route: ActivatedRoute) {
console.log(this.route.snapshot.params);
console.log(this.route.snapshot.queryParams);
}
Edit : As OP stated, we only get the initial value of the parameters with this technique.
Example Plunker
I think you should use zip operator https://www.learnrxjs.io/operators/combination/zip.html
Because if you use combineLatest and change url params or query params you got value with new query params and old url params.
Urls for example:
http://localhost/1?value=10
http://localhost/2?value=20
http://localhost/3?value=30
I managed to get a single subscription to both the queryParams and Params by combining the observables by using Observable.combineLatest
before subscribing.
Eg.
var obsComb = Observable.combineLatest(this.route.params, this.route.queryParams,
(params, qparams) => ({ params, qparams }));
obsComb.subscribe( ap => {
console.log(ap.params['type']);
console.log(ap.qparams['page']);
});
For Angular 6+
import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
...
combineLatest(this.route.params, this.route.queryParams)
.pipe(map(results => ({params: results[0].xxx, query: results[1]})))
.subscribe(results => {
console.log(results);
});
Where xxx
is from your route
{path: 'post/:xxx', component: MyComponent},
The proposed solutions using combineLatest
do not work properly. After the first route change, you will get an event every time the param
or queryParam
value changes, which means you get calls in an intermediate state when one has changed but the other has not, very bad.
@rekna's solution pretty much works for me, except I wasn't getting an event when my app was first loaded, so here's my modification to @rekna's solution:
this.routeUpdate(this.activatedRoute.snapshot);
this.routeSubscription = this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(event => this.routeUpdate(this.activatedRoute.snapshot));
Where routeUpdate
is a method to do whatever parameter handling is required.
Late answer, but another solution : Instead of subscribing to the params and queryparams I subscribe to the NavigationEnd event on the router. Fires only once and both params and queryparams are available on snapshot : (example for angular 4)
this.router.events.filter(event=>event instanceof NavigationEnd)
.subscribe(event=>{
let yourparameter = this.activatedroute.snapshot.params.yourparameter;
let yourqueryparameter = this.activatedroute.snapshot.queryParams.yourqueryparameter;
});
Regarding unsubscribing : yes it is true routing params, queryParams or navigation events subscriptions are automatically unsubscribed by the router, there is however one exception : if your route has children , when navigating between children, no unsubscribe will occur. Only when you navigate away from the parent route!
E.g. I had a situation with tabs as child routes. In constructor of first tab component subscribing on route params to retrieve data. Each time I navigate from first to second tab and back another subscription was added resulting in multiplying the number of requests.