Angular. Получить params и queryParams одновременно
Допустим необходимо из url /auth/{session}?server={id}
достать session
и id
. Для такой задачи подходит rxjs оператор combineLatest
:
combineLatest(
this._activatedRoute.params,
this._activatedRoute.queryParams
)
Он отправит массив из 2 элементов.
import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { combineLatest, Subscription } from 'rxjs';
@Component({
selector: 'my-selector',
template: `LOADING...`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyComponent implements OnInit, OnDestroy {
private _routerSubscription: Subscription;
constructor(private _activatedRoute: ActivatedRoute) {}
ngOnInit(): void {
// params забирает session, queryParams - id
this._routerSubscription = combineLatest(
this._activatedRoute.params,
this._activatedRoute.queryParams
).subscribe(([session, id]) => {
console.log(session, id);
});
}
ngOnDestroy(): void {
if (this._routerSubscription) {
this._routerSubscription.unsubscribe();
}
}
}