I would like to create a service that subscribes to changes in Angular's paramMap. Unfortunately this doesn't seem to work.
export class LocationResourcesService {
constructor(private activatedRoute: ActivatedRoute) {
this.activatedRoute.paramMap.pipe(
map((params: ParamMap) => params.get('account-id')),
).subscribe((newValue) => console.log(newValue))
}
// ----------
}
Tthe subscription above only ever emits one value - when Angular first loads into the page. And that value is null. The same subscription does when it's placed in the constructor of a component that's active in the page. presumably because the route has loaded and the ActivatedRoute has been set.
I would have assumed that ActivatedRoute was a singleton service and that I could therefore subscribe to changes on it. Obviously that's not the case though so how can this service subscribe to the value of activatedRoute.ParamMap?
There's unfortunately no easy answer to this. I'm not going to reproduce the whole discussion but the solution can be seen here: https://github.com/angular/angular/issues/11023#issuecomment-399667101.
And I'm copying it across to make it easy to get a feel for it. As I said, it's not straightforward: