Getting the navigationTrigger from the ngrx/router-store

681 Views Asked by At

I am working with ngrx/router-store 8. This version exposed the "@ngrx/router-store/request" action which include a navigationTrigger in its payload:

  type: '@ngrx/router-store/request',
  payload: {
    routerState: {
      url: '/directory',
      queryParams: {},
      params: {},
      navigationTrigger: true
    },
    event: {
      id: 2,
      url: '/jobs',
      navigationTrigger: 'imperative',
      restoredState: null
    }
  }
}

The navigationTrigger can get one of two values:

  1. imperative - for direct access to a page (by clicking on a link)
  2. popstate - when using the browser back/forward buttons

I need to know at any time how did I get to the current page so I need access to this variable. The problem is that the build-in reducer of router-store doesn't keep this variable in the state and it only shows at the payload of this action. I realize that I need to override the default reducer but I got no idea how to do this and couldn't find any information on this. Maybe there is another idea how to store this data?

1

There are 1 best solutions below

0
Wandrille On

You can listen to ROUTER_REQUEST inside an @Effect. In your payload, you will probably have the information.

  getMyNeed$ = createEffect(() => this.actions$.pipe(
    ofType(ROUTER_REQUEST),
    tap(console.log),
    map(({payload}) => SomeAction(payload))
  ));