angular - ngrx/router-store - 路由参数选择器为子路由返回 undefined

标签 angular ngrx ngrx-store ngrx-router-store

我有这个路由设置:

const SONGS_ROUTES = [
  {
    path: "songs",
    children: [
      // ...
      {
        path: "edit/:id",
        component: PerformancesComponent, // CHILD ROUTE
      },
      {
        path: "",
        component: PerformancesComponent,
      },
    ],
  },
];

const routes: Routes = [
  {
    path: "",
    component: ConcertsComponent,
    children: [
      {
        path: "edit/:friendlyUrl",
        component: ConcertEditComponent,   // PARENT route
        children: SONGS_ROUTES,
      },
    ],
  },
];

我需要能够获得 friendlyUrl在树中的每个组件中都有 ngrx 选择器。所以我定义如下:
export const routerFeatureKey = "router";

export const selectRouterState = createFeatureSelector<
  fromRouter.RouterReducerState
>(routerFeatureKey);

export const {
  selectCurrentRoute, // select the current route
  selectQueryParams, // select the current route query params
  selectQueryParam, // factory function to select a query param
  selectRouteParams, // select the current route params
  selectRouteParam, // factory function to select a route param
  selectRouteData, // select the current route data
  selectUrl, // select the current url
} = fromRouter.getSelectors(selectRouterState);

export const getSelectedConcertFriendlyUrl = selectRouteParam("friendlyUrl");

它确实在“PARENT”级别组件(路由)上工作。这意味着当用户访问 edit/some-concert 时选择器返回 一些音乐会 .但是对于/edit/some-concert/edit/songs/1 (在子组件中)它返回 undefined .我不知道为什么。

我都试过了 routerState: RouterState.MinimalrouterState: RouterState.Full .结果一样。

我可以尝试哪些新事物?

最佳答案

对于那些在网上寻找解决方案的人,我找到了在另一个 SO 线程上编写自定义选择器的替代方法。
app-routing.module.ts

@NgModule({
    imports: [RouterModule.forRoot(routes, {
        paramsInheritanceStrategy: 'always' <---- the important part
    })],
    exports: [RouterModule]
})
解决问题的原始答案:https://stackoverflow.com/a/51817329/5775417

关于angular - ngrx/router-store - 路由参数选择器为子路由返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61965415/

相关文章:

angular - 如何通过单击具有 RouterLink 指令的元素来关闭 Material SideNav?

angular - ngRx : differences between keys from state interface and keys from StoreModule. forRoot

Angular 与 Angular Universal 与 NGRX 商店

javascript - NgRX store.select 返回存储对象而不是可观察对象

node.js - Angular 5 - Node/Express - 无法下载 pdf

angular - 为什么我的 RxJS 订阅错误处理程序没有触发?

javascript - 使用带 Angular REST api 访问共享点列表给出 403

angular - @ngrx/router-store 对比@angular/router

javascript - ngrx效果并行http调用

Angular 2+/4/5/6/7 : Smart, 愚蠢和深层嵌套的组件通信