1

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?

4

1 に答える 1