Sure there is an easy way. Just make an effects proxy for handling routing actions and use the router just like any other NGRX-effects:
@Injectable()
export class RouterEffects {
@Effect({ dispatch: false })
navigate$ = this.actions$.ofType(RouterActions.GO)
.map((action: RouterActions.Go) => action.payload)
.do((routerPayload: RouterPayload) => {
this.router.navigate(routerPayload.path, routerPayload.query);
});
@Effect({ dispatch: false })
navigateBack$ = this.actions$.ofType(RouterActions.BACK)
.do(() => this.location.back());
@Effect({ dispatch: false })
navigateForward$ = this.actions$.ofType(RouterActions.FORWARD)
.do(() => this.location.forward());
constructor(
private actions$: Actions,
private router: Router,
private location: Location
) {}
}
with the router.actions.ts:
export const GO = '[Router] Go';
export const BACK = '[Router] Back';
export const FORWARD = '[Router] Forward';
export interface RouterPayload {
path: string[];
query?: NavigationExtras;
}
export class Go implements Action {
readonly type = GO;
constructor(public payload: RouterPayload) {}
}
export class Back implements Action {
readonly type = BACK;
}
export class Forward implements Action {
readonly type = FORWARD;
}
export type Actions
= Go
| Back
| Forward;
and in your component you dispatch router actions on navigation instead of calling routerLink directly in the template:
navigate(routerPayload: RouterPayload) {
this.store.dispatch(new RouterActions.Go(routerPayload));
}
This way you can also easily go back and forth in your routes.