Platform: @ngrx/router-store selectRouteData returns always empty

Created on 21 Aug 2020  路  10Comments  路  Source: ngrx/platform

Minimal reproduction of the bug/regression with instructions:

Reproduced: https://stackblitz.com/edit/ngrx-seed-s6jcdy?file=src%2Fapp%2Ftest.component.ts

I've followed all the steps from the https://ngrx.io/guide/router-store, but selectRouteData doesn't return data from routes resolvers. Am I missing something that is not documented?

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

  readonly center$ = this.store.select(selectRouteData).pipe(
    tap(data => {
      console.log('selectRouteData', data, this.route.snapshot);
    }),
  );

But data exists in the ActivatedRoute snapshot.

Screen Shot 2020-08-21 at 12 03 30

Expected behavior:

selectRouteData should return data from activated route.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):

Latest versions.

Router Store

Most helpful comment

So the problem are resolvers.

const routes: Routes = [
  {
    path: '',
    component: TestComponent,
    // 馃憞 this doesn't get "selected" with `selectRouteData`
    resolve: {
      test: TestResolver,
    },
    // 馃憞 this gets "selected"  with `selectRouteData`
    data: {
      id: 1
    }
  },
];

All 10 comments

Do you got a reproduction for this @greg-md ?

@timdeschryver is there an active template for ngrx version 10 in stackblitz? I couldn't find one. Basically I did everything from the docs.

  1. Installed StoreRouterConnectingModule is app module;
StoreModule.forRoot(appReducers),
StoreRouterConnectingModule.forRoot(),
  1. Added routerReducer in the app reducers.
import { ActionReducerMap } from '@ngrx/store';
import { routerReducer, RouterReducerState } from '@ngrx/router-store';

export type AppState = {
  router: RouterReducerState<any>;
};

export const appReducers: ActionReducerMap<AppState> = {
  router: routerReducer,
};
  1. Exported router selectors:
export const selectRouter = createFeatureSelector<AppState, RouterReducerState>('router');

export const {
  selectRouteData,      // select the current route data
} = getSelectors(selectRouter);
  1. Then tried to select route data:
readonly data$ = this.store.select(selectRouteData);

And it's always empty, but I have resolvers inside the current router and I could get data from activated route snapshot.

@greg-md there is this one, https://stackblitz.com/edit/ngrx-seed?file=src/app/app.component.ts.
It's a basic template, with only one rout but the set up is done for all the packages.

So the problem are resolvers.

const routes: Routes = [
  {
    path: '',
    component: TestComponent,
    // 馃憞 this doesn't get "selected" with `selectRouteData`
    resolve: {
      test: TestResolver,
    },
    // 馃憞 this gets "selected"  with `selectRouteData`
    data: {
      id: 1
    }
  },
];

@timdeschryver right. I think they should be included as well in selectRouteData

I'm not very familiar with it, but it seems that Angular doesn't expose the data from the resolved in the router events. So if Angular doesn't expose it, I don't know if we can...

image

@greg-md do you know how we could include the data in the payload?

related to #358

navigationActionTiming: NavigationActionTiming.PostActivation

@greg-md Adding this config seems to help but not sure regarding the remarks by Brandon in 358.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gperdomor picture gperdomor  路  3Comments

oxiumio picture oxiumio  路  3Comments

ghost picture ghost  路  3Comments

Matmo10 picture Matmo10  路  3Comments

mappedinn picture mappedinn  路  3Comments