3 回答

TA貢獻1820條經(jīng)驗 獲得超9個贊
因此,如果您想從一個效果中分派多個動作,您可以返回一組動作并將您的替換map為mergeMap:
@Effect()
uploadSpecChange$: Observable<Action> = this.actions$.pipe(
ofType(AppActions.WorkspaceActionTypes.UploadChange),
switchMap((x: any) => {
let f = new FileReader();
f.readAsText(x.payload);
return fromEvent(f, 'load');
}),
mergeMap((result: any) => {
const raw = (<FileReader>result.target).result as string;
const res = JSON.parse(raw);
// console.log(res);
return [
new AppActions.UploadComplete(res),
new AppActions.PerformCalc()
];
})
);
但是,如果您希望在PerformCalc操作完成后分派該UploadComplete操作,您應(yīng)該分派PerformCalc作為 的副作用UploadComplete:
@Effect()
uploadSpecChange$: Observable<Action> = this.actions$.pipe(
ofType(AppActions.WorkspaceActionTypes.UploadChange),
switchMap((x: any) => {
let f = new FileReader();
f.readAsText(x.payload);
return fromEvent(f, 'load');
}),
map((result: any) => {
const raw = (<FileReader>result.target).result as string;
const res = JSON.parse(raw);
// console.log(res);
return new AppActions.UploadComplete(res);
})
);
@Effect()
uploadComplete$: Observable<Action> = this.actions$.pipe(
ofType(AppActions.WorkspaceActionTypes.UploadComplete),
map(() => new AppActions.PerformCalc())
);

TA貢獻1810條經(jīng)驗 獲得超4個贊
多個動作返回一個@Effect,
你需要使用concatMap而不是map
concatMap((result: any) => {
const raw = (<FileReader>result.target).result as string;
const res = JSON.parse(raw);
return [
new AppActions.UploadComplete(res),
new AppActions.PerformCalc()
]
})

TA貢獻1998條經(jīng)驗 獲得超6個贊
您可以使用 switchMap & of operator 這樣做
@Effect()
dispatchMultiAction$: Observable<Action> = this.actions$.pipe(
ofType<SomeAction.Dispatch>(someActions.Dispatch),
switchMap(_ =>
of(
new someActions.InitData(),
new someActions.GetData(),
new someActions.LoadData()
)
)
);
添加回答
舉報