使用路由器存储在效果模块中导航时,如何获得激活的路线?

JSO*_*SON 0 angular-routing ngrx ngrx-router-store

我正在尝试调度相对于组件路线的路由器商店导航操作。路由跟踪日志显示,用于导航的相对路径是应用程序路径而不是组件路径。

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

 constructor(
    private actions$: Actions,
    private route: ActivatedRoute,
  ) {}
Run Code Online (Sandbox Code Playgroud)

我正在ActivatedRoute效果模块构造函数中注入,这似乎是这里的问题。我如何才能在组件之外获取激活的路线并将其传递给导航动作?

Eri*_*c S 5

ActivatedRoute特定于每个路由组件。

这给您几个选择。

  1. 浏览根路由以返回所需的数据。

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

return route.snapshot;
Run Code Online (Sandbox Code Playgroud)

  1. 从包中的路由组件传递ActivatedRoute。

  constructor(private store: Store<AppState>, private activatedRoute:ActivatedRoute) {}

  doSomething() {
    this.store.dispatch(new Action.YourAction({ id: actionId, route: activatedRoute });
  }
  
 
Run Code Online (Sandbox Code Playgroud)