Search code examples
angularangular2-routingngrx

Dispatch an action when a route become active


I have several routes each with a table that should load data from the server. How can I dispatch an action to the ngrx/store to load data from the server when a route with table become active?

Thanks


Solution

  • You can use the @ngrx/router-store that dispatches actions on different lifecycle steps.

    Then add an effect that listen to ROUTER_NAVIGATED for example filters on a specific location and dispatches the needed loading action.

    @Effect()
    $routerNavigated = this.actions$.ofType(ROUTER_NAVIGATED)
    .pipe(filter(action => (/persons/g).test(action.payload.url)),
    map(action =>  new LoadSomePersonsData()));