2

Here is a StackBlitz of the problem I'm facing:

https://stackblitz.com/edit/angular-ivy-aetvyf?file=src%2Fapp%2F

In AppComponent I'm subscribing to ActivatedRoute.url changes, but they only ever trigger once.

Shouldn't ActivatedRoute.url subscription emit on every route change?

I know I can use Router.events, but there you have to filter by event type and Router events don't have route segments, as ActivatedRoute.snapshot, for instance, does.

I have read all related questions, but none really answer the question.

Suraj Gupta
  • 437
  • 8
  • 19
piculence
  • 41
  • 3
  • Does this answer your question? https://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular – Aakif Sep 14 '20 at 17:49
  • you should listen for activated on your navigated component, not on root component. – Suraj Gupta Sep 14 '20 at 18:57

1 Answers1

1

I have seen your code. You must subscribe on you child components i.e. a and b to detect the url changes. Currently you are adding these subscriptions on app.component. Moreover, if you want one app level solution, you have to subscribe to Router.events.

Like this :-

constructor(private ar: ActivatedRoute, private r: Router) {
  this.r.events.subscribe((segs) => {
    console.log('Route segments');
    console.log(segs);
  });
}
d1fficult
  • 931
  • 8
  • 18