I am currently using ngrx in my angular 12 app. I was trying to understand how ngrx-router and ngrx-data can be used together.
I have several files:
app.module.ts (interesting lines)
/**
* Store Modules
*/
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({ maxAge: 25 }),
EffectsModule.forRoot([RouterEffects]),
StoreRouterConnectingModule.forRoot({ routerState: RouterStateMinimal }),
EntityDataModule.forRoot(entityConfig),
article-collections.service.ts
import { Injectable } from '@angular/core';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory,
} from '@ngrx/data';
import { Observable } from 'rxjs';
import { Article } from '../models/articles/article';
import * as fromArticle from '../store/article/article.selectors';
@Injectable({ providedIn: 'root' })
export class ArticleCollectionService extends EntityCollectionServiceBase<Article> {
active$: Observable<Article>;
constructor(elementsFactory: EntityCollectionServiceElementsFactory) {
super('Article', elementsFactory);
this.active$ = this.store.select(fromArticle.selectActive);
}
}
index.ts
import { routerReducer, RouterReducerState } from '@ngrx/router-store';
import { ActionReducerMap, MetaReducer } from '@ngrx/store';
import { navigationReducer } from './navigation/navigation.reducer';
import { NavigationState } from './navigation/navigation.state';
export interface RootState {
router: RouterReducerState;
navigation: NavigationState;
}
export const reducers: ActionReducerMap<RootState> = {
router: routerReducer,
navigation: navigationReducer,
};
export const metaReducers: MetaReducer[] = [];
router.selectors.ts
import { getSelectors } from '@ngrx/router-store';
import { RootState } from '..';
export const selectFeature = (state: RootState) => state.router;
export const {
selectCurrentRoute,
selectFragment,
selectQueryParams,
selectQueryParam,
selectRouteParams,
selectRouteParam,
selectRouteData,
selectUrl,
} = getSelectors(selectFeature);
article.selectors.ts
import { EntitySelectorsFactory } from '@ngrx/data';
import { createSelector } from '@ngrx/store';
import { Article } from '../../models/articles/article';
import * as fromRouter from '../router/router.selectors';
export const {
selectEntities,
selectEntityMap,
} = new EntitySelectorsFactory().create<Article>('Article');
export interface ArticleStats {
total: number;
}
export const selectStats = createSelector(
selectEntities,
(articles): ArticleStats => {
return {
total: articles.length,
};
}
);
export const selectActiveId = fromRouter.selectRouteParam('id');
export const selectActive = createSelector(
selectEntityMap,
selectActiveId,
(entities, id) => entities[id!]
);
In the last file i added entities[id!]
because selectRouteParam('id')
give me undefined as possible result. I guess it make sense since the url id will not always be defined
this line this.active$ = this.store.select(fromArticle.selectActive);
produces the following error
No overload matches this call.
Overload 1 of 9, '(mapFn: (state: EntityCache) => Article | undefined): Observable<Article | undefined>', gave the following error.
Argument of type 'MemoizedSelector<RootState, Article | undefined, DefaultProjectorFn<Article | undefined>>' is not assignable to parameter of type '(state: EntityCache) => Article | undefined'.
Types of parameters 'state' and 'state' are incompatible.
Type 'EntityCache' is missing the following properties from type 'RootState': router, navigation
Overload 2 of 9, '(key: string | number): Observable<EntityCollection<any>>', gave the following error.
Argument of type 'MemoizedSelector<RootState, Article | undefined, DefaultProjectorFn<Article | undefined>>' is not assignable to parameter of type 'string | number'.
Type 'MemoizedSelector<RootState, Article | undefined, DefaultProjectorFn<Article | undefined>>' is not assignable to type 'number'.
In my article-collections.service.ts
i get this error that i don't understand. After looking up it seems to be related to a wrong interface declarations, but i don't manage to understand where i did something wrong.
I'm not sure if you're already doing this, but I'd recommend to create a custom url serializer so you're more in control of what's being sent to the router store.
custom-url-serializer.ts
import { RouterStateSerializer } from '@ngrx/router-store';
import { Params, RouterStateSnapshot } from '@angular/router';
export interface CustomRouterStateUrl {
url: string;
params: Params;
queryParams: Params;
}
export class CustomSerializerUrl implements RouterStateSerializer<CustomRouterStateUrl> {
serialize(routerState: RouterStateSnapshot): CustomRouterStateUrl {
let route = routerState.root;
while (route.firstChild) {
route = route.firstChild;
}
const { url, root: { queryParams }} = routerState;
const { params } = route;
return { url, params, queryParams };
}
}
You need to use this serializer in your router store selector
import { CustomRouterStateUrl} from './custom-url-serializer';
import { RouterReducerState } from '@ngrx/router-store';
import { createFeatureSelector, createSelector } from '@ngrx/store';
export const getRouterState = createFeatureSelector<
RouterReducerState<CustomRouterStateUrl>
>('router');
export const getCurrentRoute = createSelector(getRouterState, (router) => {
return router.state;
});