reducer.ts
export interface AppState {
router: fromRouter.RouterReducerState;
}
export const reducers: ActionReducerMap<AppState> = {
router: fromRouter.routerReducer
};
export const getRouterState = (state: AppState) => state.router;
export const getCurrentUrl = createSelector(getRouterState, (state: fromRouter.RouterReducerState) => state.state.url);
app.component.ts
constructor(
private store: Store<fromRoot.AppState>
) {
store.select(fromRoot.getCurrentUrl).subscribe(url => console.log(url));
}
When I do this, I get an error saying that it cannot read 'state' of undefined.
I tried implementing the solution explained in #151, but it did not work for me either.
Simply returning router state as a whole, and logging it to console shows me an object, but whenever I attempt accessing its properties, they are shown as 'undefined', which is weird because I am able to subscribe or get a value of state using the method shown above.
The router state has an initial state of undefined, so you need to protect against that in your selector.
export const getCurrentUrl = createSelector(getRouterState, (state: fromRouter.RouterReducerState) => state.state && state.state.url);
Hi, How i can unsubscribe ngrx router selector
Most helpful comment
The router state has an initial state of undefined, so you need to protect against that in your selector.