2017-03-14 13 views
0

これは私の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> 

答えて

1

私はあなたがここにmapを誤解だと思う:

this.store$.select(fromRoot.getPlanEntities) 
    .map(plan => { 
     if (plan.id != null) 
     { 
      return {id: plan.id, text: plan.name}; 
     } 
    }); 

それはES6 mapしかしObservablemapではありません。 あなたは、単にあなたが一方のエンティティを取得したい場合は、あなたがそのようなことをすべきあなたのplanEntities を取得している: `プロパティ「マップ」タイプ「IPlanに存在しません:私はこのコンパイラのメッセージが出てい

this.store$.select(fromRoot.getPlanEntities) 
    .map(planEntities => { 
    return planEntities 
     .find(plan => plan.id !== null) 
     .map(plan => ({ id: plan.id, text: plan.name })); 
    }) 
    .do((plan: {id: any, text: string}) => /* DO WHATEVER YOU WANT HERE */); 
+0

を'' - > 'find(...)。map()'。 – Jordi

+0

私はちょうど私が得ようとしているものについてもう少し情報を与えるために投稿を編集しました... – Jordi

関連する問題