これは私のIStore
です:ngrx:セレクタ型マッピング
IPlanRedux
がある
export interface IStore {
plans: IPlanRedux;
}
:
だから、export interface IPlanRedux {
entities: { [key: string]: IPlan };
ids: Array<string>;
}
export interface IPlan {
id?: string;
name?: string;
}
、私は私の状態からセレクタを作成する必要があります。私が作成したセレクタは、以下のとおりです。
plan.selectors.ts
:
export const getEntities = (planRdx: IPlanRedux) => planRdx.entities;
export const getIds = (planRdx: IPlanRedux) => planRdx.ids;
export const getAll = createSelector(getEntities, getIds, (entities, ids) => { return ids.map(id => entities[id]); });
とroot.selectors.ts
上:
export const getPlansState = (state: IStore) => state.plans;
export const getPlanEntities = createSelector(getPlansState, fromPlans.getAll);
私は私が解決する方法がわからないコンパイラエラーで立ち往生してきました。私はすべてのエンティティを取得するために、私のコンポーネントにgetPlanEntities
セレクタを使用してサブスクリプションを作成しようとしている:
this.store$.select(fromRoot.getPlanEntities)
.map(plan => {
if (plan.id != null)
{
return {id: plan.id, text: plan.name};
}
});
問題は、パラメータの型です。コンパイラは、IPlan
の代わりにのタイプがIPlan[]
であると伝えます。だから、私はProperty 'id' does not exist on type 'IPlan[]'
のようなコンパイルエラーを受けているのではの代わりにIPlan[]
です。
この問題の対処方法はわかりません。単一の配列ではなく、単一のエンティティを1つずつ取得する方法はありますか?
EDIT
私が達成したい目標はthis.store$.select(fromRoot.getPlanEntities)
から私のコンポーネントにObservable<IPlan>
を作ることです。
private plans$: Observable<IPlan>;
constructor(private store$: Store<IStore>, private fb: FormBuilder, private router: Router, injector: Injector)
{
this.plans$ = this.store$.select(fromRoot.getPlanEntities)
.map(planEntities => {
return planEntities
.find(plan => plan.id != null)
.map(plan => ({id :plan.id, text: plan.name}));
});
...
そして、すべての現在の計画をリストアップするために、テンプレートにそれを使用する:
<div class="clearfix">
<div *ngIf="plans$ | async">
<div [innerHTML]="(plans$ | async).name"></div>
</div>
を'' - > 'find(...)。map()'。 – Jordi
私はちょうど私が得ようとしているものについてもう少し情報を与えるために投稿を編集しました... – Jordi