Home > OS >  Subscribe to changes to a part of the router URL
Subscribe to changes to a part of the router URL

Time:02-01

Say I have a link localhost:4200/details/1 I want to subscribe only to changes in the "details" part of the URL. i.e. when "details" changes to some other word and not the other parts of the URL. How do I do it?

CodePudding user response:

You have to

  1. Subscribe to all changes
  2. Map to required property
  3. use distinctUnilChanged operator

eg.

private route:ActivatedRoute
    this.route.params.pipe(
      map(params => params.yourParamName),
      distinctUntilChanged(),
    ).subscribe(changedParam => console.log(`reacting to single param change ${changedParam}`));

CodePudding user response:

You can subscribe to the activatedRoute url, and just get the first segment. Ref. enter image description here

  •  Tags:  
  • Related