Search code examples
angular-routingngrxngrx-router-store

How can I get activated route when navigation in effect module using the router store?


I am trying to dispatch the router store navigation action relative to the component route. the trace log for routing shows that the relative path taken for navigation is the app-path instead of the component path.

 return of(
        new routerActions.Go({
          path: ['./', payload.rootPath],
          extras: { relativeTo: this.route },
        }),
      );

 constructor(
    private actions$: Actions,
    private route: ActivatedRoute,
  ) {}

I am injecting the ActivatedRoute in my effect module constructor which seems the issue here. How can I get the activated route outside of my component and pass it to the navigation action?


Solution

  • ActivatedRoute is specific to each routed component.

    This gives you a couple options.

    1. Navigates the root route to return the data you need.

    let route = this.router.routerState.root;
    while (route.firstChild) {
    route = route.firstChild;
    }
    
    return route.snapshot;

    1. Pass the ActivatedRoute from the routed component in the package.

      constructor(private store: Store<AppState>, private activatedRoute:ActivatedRoute) {}
    
      doSomething() {
        this.store.dispatch(new Action.YourAction({ id: actionId, route: activatedRoute });
      }