0

这是我的代码:

@Injectable()
export class TraitementDetailEffects {
    ingoing_loadDetail: { traitementID: number, obs: Promise<any> };
    @Effect()
    loadTraitementDetail$: Observable<Action> = this.actions$.pipe(
        ofType(ETraitementDetailActions.loadTraitementDetail),
        map((action: LoadTraitementDetail) => action.payload),
        switchMap((traitementID) => {
            if (this.ingoing_loadDetail && this.ingoing_loadDetail.traitementID === traitementID) {
                return this.ingoing_loadDetail.obs;
            }
            const obs = this.traitementsService.loadDetail(traitementID);
            this.ingoing_loadDetail = {traitementID: traitementID, obs: obs};
            return obs;
        }),
        map(result => {
            this.ingoing_loadDetail = null;
            //here I don't have access to traitementID :'(
            return new LoadTraitementDetailSuccess(traitementID, result);
        })
    );

    constructor(
        private actions$: Actions,
        private traitementsService: TraitementsService
    ) {

    }
}

我正在尝试将变量或值 traitementID 传递给最后一张地图。

我试图通过异步等待避免最后一张地图,但后来我收到一个奇怪的错误“效果调度了一个无效的动作”和“动作必须有一个类型属性”(仅供参考,我所有的动作都有一个类型属性)。

4

1 回答 1

1

尝试将此 id 烘焙到 observable 的解析中,例如:

            switchMap((traitementID) => {
                return this.traitementsService.loadDetail(traitementID).pipe(
                           map(detail => ({detail,traitementID}))
                       );

            }),
            map(({detail,traitementID}) => {
                ...
            })
于 2019-08-02T12:15:41.033 回答