0
votes

In my component where routing is occurring (and where router outlet resides), I'm able to use ActivatedRoute just like usual, but I have a component that stays static between route changes, and it needs to know the current route and route parameters. In the Angular docs here, it says:

You can access the current RouterState from anywhere in the application using the Router service and the routerState property.

But I'm not sure the page explains just how to do that. It doesn't mention a routerState property anywhere.

1

1 Answers

1
votes

Assuming you inject ActivatedRoute to your static component:

constructor(private route: ActivatedRoute) {}

You then can listen to route params or query params changes by subscribing to the two corresponding Observable in ActivatedRoute. By this way, the static component will always be updated with the latest route parameters.

ngOnInit() {
  this.route.params.subscribe(params => {console.log(params);});
  this.route.queryParams.subscribe(qParams => {console.log(qParams);});
}