I'm not sure of that answers above that state it's not possible to read ActivatedRoute in something different from a Component. I'm using this class (that's even not a Service, just a raw class that uses Angular DI) and it worked. It's true that ActivatedRoute has to have been activated before using it, but it's straightforward. Here is my code to find the last child in the ActivatedRoute and I assure you it works:
import { Inject } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { Observable, of, zip } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { EResolvedorParametros } from '../enums/resolvedor-parametros.enum';
import { IResolvedorParametros } from '../interfaces/resolvedor-parametros.interface';
export class ResolvedorRouter implements IResolvedorParametros {
public constructor(@Inject(ActivatedRoute) private route: ActivatedRoute) { }
public intentarResolverParametro(parametro: string): Observable<string> {
let ultimaRuta: ActivatedRoute = this.route;
while (ultimaRuta.firstChild) {
ultimaRuta = ultimaRuta.firstChild;
}
return zip(
ultimaRuta.paramMap.pipe(switchMap((params: ParamMap): Observable<string> => {
return of(params.get(parametro));
})),
ultimaRuta.queryParamMap.pipe(switchMap((params: ParamMap): Observable<string> => {
return of(params.get(parametro));
}))
).pipe(switchMap((resultados: string[]): Observable<string> => {
let retorno: string = null;
if (resultados) {
retorno = resultados.find((resultado: string) => !!resultado);
}
return of(retorno);
}));
}
}
In that code I'm searching for parameters in the url from both, path and query string, and I'm not using that query string in my tests and it always finds my parameters.
Hope this helps.