【问题标题】:NGRX return array in effectNGRX 返回数组生效
【发布时间】:2020-03-24 00:30:36
【问题描述】:

当我尝试运行以下代码时:

functionMap = {
[ActionTypeEnum.TYPE_FIRST.toLowerCase()]:[getInitialDataStatus({statusType: 'disabled'}),fetchImportantDataFirst()],
[ActionTypeEnum.TYPE_SECOND.toLowerCase()]:[getInitialDataStatus({}),
                        fetchImportantDataSecond()],
[ActionTypeEnum.TYPE_THIRD.toLowerCase()]:[getInitialDataStatus({}),
                        fetchImportantDataThidr()]
}

 startMultpileActionDepensOnType$ = createEffect(() => this.actions$.pipe(
        ofType(startMultpileActionDepensOnType),
        withLatestFrom(this.store$.select(selectTypeActions)),
        map(([action, select]) => {
                const actionType = select.type.toLowerCase();
                return this.unctionMap[actionType]

            }
        )
    ));

我收到以下错误:

TS2322: Type 'Observable<TypedAction<string>[]>' is not assignable to type 'Observable<Action>'. Property 'type' is missing in TypedAction<string>[].

但是当我改为:functionMap:{}

我得到了错误:

Action must have a type property

【问题讨论】:

    标签: angular typescript rxjs ngrx effect


    【解决方案1】:

    effect 内部,要检索多个动作,所以一个动作数组,你必须使用switchMap 运算符:

    load$ = createEffect(() => this.actions$.pipe(
      ofType(actions.load),
      switchMap(action => [
        actions.loadItems(),
        actions.loadHistory();
      ])
    ))
    

    所以在你的情况下,functionMap 返回一个动作数组:

    startMultipleActionDepensOnType$ = createEffect(() => this.actions$.pipe(
      ofType(startMultipleActionDepensOnType),
      withLatestFrom(this.store$.select(selectTypeActions)),
      switchMap(([action, select]) => {
        const actionType = select.type.toLowerCase();
        return this.functionMap[actionType];
      })
    ));
    

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多