I'm trying to understand the way of creating a route, with some information in it's URL parameters.
This is my route (app.routes.ts):
{path: 'results/:id', component: MyResultsComponent},
This is how I navigate to the route :
goToResultsPage(query: string) {
this.router.navigate(['results', query], { queryParams: { pageSize: 20 } });}
As you can see, I've also a query parameter. I was wondering, what is the best and cleanest way to retrieve this in my MyResultsComponent
. Right now I've kind of a nested subscribe
structure:
ngOnInit() {
this.route
.params
.subscribe(params => {
this.query = params['id'];
this.route
.queryParams
.subscribe(queryParams => {
this.offset = queryParams['pageSize'];
#find entries this.entryService.findEntries(this.query, this.pageSize);
});
});
}
Afterwards, I want to pass this parameters to my EntryService
, which returns the found entries.
The Observable.combineLatest is a solution in this situation, isn't it?
I've faced this dilemma recently and the best solution fitting my needs I've found is to use
Observable.merge
.Using snapshot is not good if you want to watch changes from params queryParams within the same component (i.e search with filters).
Observable.forkJoin
has the limitation that both observables must be triggered in order to work, so if you have a scenario where there might be changes in params or in queryParams, but not necessarily in both, forkJoin wouldn't hep you.Observable.merge
on the other hand is triggered with either one of them being triggered. Of course, this also has some drawbacks since you might have two callbacks being called one after the other (at the beginning with params and queryParams).Here is an example:
What about using a forkJoin?
ForkJoin allows you to join two observables and wait for the response of both them. You can see the documentation here and read more about it here.
As for your code it would look something like this:
You might need to add imports:
I guess if you need to access both at the same time the snapshot is a better option.
Not tested (just from the top of my head)