Bindings to connect the Angular Router to @ngrx/store
Bindings to connect angular/router to ngrx/store
npm install @ngrx/[email protected] --save
routerReducer
when providing the StoreModule.provideStore
and add the RouterStoreModule.connectRouter
to the @NgModule.imports
:import { StoreModule } from '@ngrx/store';
import { routerReducer, RouterStoreModule } from '@ngrx/router-store';
@NgModule({
imports: [
BrowserModule,
StoreModule.provideStore({ router: routerReducer }),
RouterStoreModule.connectRouter()
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}
RouterState
to main application state:import { RouterState } from '@ngrx/router-store';
export interface AppState {
router: RouterState;
};
StoreModule.provideStore({ router: routerReducer }, {
router: {
path: window.location.pathname + window.location.search
}
})
import { go, replace, search, show, back, forward } from '@ngrx/router-store';
store.dispatch(go(['/path', { routeParam: 1 }], { query: 'string' }));
store.dispatch(replace(['/path'], { query: 'string' }));
store.dispatch(show(['/path'], { query: 'string' }));
store.dispatch(search({ query: 'string' }));
store.dispatch(back());
store.dispatch(forward());
The Angular Router Navigation Extras are supported with each router action.
import { NavigationExtras } from '@angular/router';
let extras: NavigationExtras = {
relativeTo: ActivatedRoute,
fragment: string,
preserveQueryParams: boolean,
preserveFragment: boolean,
skipLocationChange: boolean,
replaceUrl: boolean
};
store.dispatch(go(['path', { routeParam: 1 }], { query: 'string' }, extras));