I have a (login) component that gets state passed with the route. As far as I know, passed state can be read via Router.router.getCurrentNavigation()?.extras.state
in the constructor and via history.state
in other code, like in ngOnInit
. This works if the component is being instatiated for the current route.
However I have a case where the component is not being instatiated for a new route because an instance already exists.
My question is how the component can get informed about changes in the state passed with the new route. I could not find any event I could use (like with ActivatedRoute.paramMap.subscribe(...)
for route parameters). I found that history.state
does not store my own state in routing events, hence events could not be used for my problem.
My use case is: A route is guarded by a authentication route card, passed to canResolve
in the route definition. The route guard navigates to /login if the user is not authenticated. The return URL should be passed via state (not via a parameter). If the user klicks on one links for a guarded route (in the navigation bar), the login component gets the return URL from the state. If the user then clicks on another guarded link, the login component does not get the updated return URL.
I know that using a route parameter would solve my problem, but this is not the point. I generally want to know if a component can react to changes in passed route state.