angular ngrx存储将多http服务调用为one-effect和dispacth操作



嗨,我需要以并行模式调用multi-http服务,为了实现这一目标,我使用forkJoin,但当它完成时,它不会调度actsions。

doSearchCliente$ = createEffect(() =>
this.actions$.pipe(
ofType(addCliente),
switchMap(action => {
const cliente$ = this.clienteService.ClienteById(action.id).pipe(
map(response => addClienteSuccess({ response })),
catchError(() => of(addClienteFailure())));
const listino$ = this.clienteService.ListinoCollection(action.id).pipe(
map(response => addListinoSuccess({ response })),
catchError(() => of(addListinoFailure())));
return forkJoin([cliente$, listino$]).pipe(
mergeMap(response => [response[0], response[1]])
);
}),
));

有人能帮我吗?

尝试在每个http请求的每个映射之后添加第一个oeprator。forkJoin在所有Observable结束时发出。当您执行http请求时,从技术上讲,可观测性不会结束。所以第一个操作符就结束了。

doSearchCliente$ = createEffect(() =>
this.actions$.pipe(
ofType(addCliente),
switchMap(action => {
const cliente$ = this.clienteService.ClienteById(action.id).pipe(
first(),
map(response => addClienteSuccess({ response })),
catchError(() => of(addClienteFailure())));
const listino$ = this.clienteService.ListinoCollection(action.id).pipe(
first(),
map(response => addListinoSuccess({ response })),
catchError(() => of(addListinoFailure())));
return forkJoin([cliente$, listino$]).pipe(
mergeMap(response => [response[0], response[1]])
);
}),
));

你是这么说的吗?

doSearchCliente$ = createEffect(() =>
this.actions$.pipe(
ofType(addCliente),
switchMap(action => {
const cliente$ = this.clienteService.ClienteById(action.id).pipe(
map(response => addClienteSuccess({ response })),
catchError(() => of(addClienteFailure())));
const listino$ = this.clienteService.ListinoCollection(action.id).pipe(
map(response => addListinoSuccess({ response })),
catchError(() => of(addListinoFailure())));
return forkJoin([cliente$]);
}),
switchMap(response => {
return [response[0]];
})
));

无效!:(

相关内容

最新更新