我目前在我的 Angular 12 应用程序中使用 ngrx。我试图了解如何将ngrx-router 和ngrx-data 一起使用。
我有几个文件:
app.module.ts(有趣的行)
/**
* Store Modules
*/
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({ maxAge: 25 }),
EffectsModule.forRoot([RouterEffects]),
StoreRouterConnectingModule.forRoot({ routerState: RouterStateMinimal }),
EntityDataModule.forRoot(entityConfig),
文章-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);
}
}
索引.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[] = [];
路由器选择器.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);
文章.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!]
);
在我添加的最后一个文件中,entities[id!]
因为selectRouteParam('id')
给我未定义的可能结果。我想这是有道理的,因为 url id 并不总是被定义
此行this.active$ = this.store.select(fromArticle.selectActive);
产生以下错误
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'.
在我的article-collections.service.ts
我得到这个我不明白的错误。查找后似乎与错误的接口声明有关,但我不明白我做错了什么。