NGRX return 数组生效
NGRX return array in effect
当我尝试 运行 下面的代码时:
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
在 effect
中,要检索多个动作,所以动作数组,您必须使用 switchMap
运算符:
load$ = createEffect(() => this.actions$.pipe(
ofType(actions.load),
switchMap(action => [
actions.loadItems(),
actions.loadHistory();
])
))
因此,在您的情况下,functionMap
returns 一系列操作:
startMultipleActionDepensOnType$ = createEffect(() => this.actions$.pipe(
ofType(startMultipleActionDepensOnType),
withLatestFrom(this.store$.select(selectTypeActions)),
switchMap(([action, select]) => {
const actionType = select.type.toLowerCase();
return this.functionMap[actionType];
})
));
当我尝试 运行 下面的代码时:
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
在 effect
中,要检索多个动作,所以动作数组,您必须使用 switchMap
运算符:
load$ = createEffect(() => this.actions$.pipe(
ofType(actions.load),
switchMap(action => [
actions.loadItems(),
actions.loadHistory();
])
))
因此,在您的情况下,functionMap
returns 一系列操作:
startMultipleActionDepensOnType$ = createEffect(() => this.actions$.pipe(
ofType(startMultipleActionDepensOnType),
withLatestFrom(this.store$.select(selectTypeActions)),
switchMap(([action, select]) => {
const actionType = select.type.toLowerCase();
return this.functionMap[actionType];
})
));