vsavkin / state_management_ngrx4

206 stars 55 forks source link

Guard and ROUTER_NAVIGATION execution order #3

Closed felikf closed 6 years ago

felikf commented 7 years ago

I have a question - this is not a code issue. I like the ngrx-router approach but there are some circumstances that I do not know how to solve:

During the navigation, before any guards or resolvers run, the router will dispatch a ROUTER_NAVIGATION action, which has the signature RouterNavigationAction

(source: https://github.com/ngrx/platform/tree/master/docs/router-store)

How to combine ROUTER_NAVIGATION with a route guard :

private handleNavigation(segment: string, callback: (a: ActivatedRouteSnapshot, state: State) => Observable<any>) {
    const nav = this.actions.ofType(ROUTER_NAVIGATION).
    map(firstSegment).
    filter(s => s.routeConfig.path === segment);

    return nav.withLatestFrom(this.store).switchMap(a => callback(a[0], a[1])).catch(e => {
        console.log('Network error', e);
        return of();
    });
}
}

with for example AuthGuard which is executed after ROUTER_NAVIGATION action but I want to guard the segment here or I need some data that are fetched asynchronously and are not yet in the store (request has been excuted, data have not arrived yet - a data guard - again executed after ROUTER_NAVIGATION).

felikf commented 7 years ago

Also question posted here: https://stackoverflow.com/questions/47202946/angular-router-guard-and-router-navigation-effect-order

felikf commented 7 years ago

@vsavkin Victor any idea how to deal with this?

felikf commented 6 years ago

Here is a nice way to handle this issue: https://medium.com/@amcdnl/angular-routing-data-with-ngrx-effects-1cda1bd5e579