angularngrx-router-storengrx-store-8.0

Using @ngrx/router-store actions with ngrx 8 createReducer() and on() function


I have a ngrx v8 style reducer (using createReducer + on) that needs to react to route changes.

let myAcction = createAction('my action', prop<boolean>);

let reducer = createReducer(
  initialState,
  on(myAction, (state, prop) => ({...state, example=prop })),
  on(ROUTER_NAVIGATION, (state, routeParams) => ({...state, example2:{...routeParams}}))
)

This fails to compile because ROUTER_NAVIGATION is not action creator:

Argument of type '"@ngrx/router-store/navigation"' is not assignable to parameter of type 'ActionCreator>'. Type '"@ngrx/router-store/navigation"' is not assignable to type 'FunctionWithParametersType'

It does not work with RouterNavigationAction either:

Argument of type 'boolean' is not assignable to parameter of type 'ActionCreator>'

How can I use ngrx/router-store actions in createReducer/on functions? I can't find action creators exported for given actions.


Solution

  • I would file an issue against ngrx on their repository. In the meantime, you can hack around it by using the token in an object: { type: ROUTER_NAVIGATION } since this is how it works under the hood. It's not ideal, but it works until they fix it.