angular ngrx store call multi http service into a effect and dispacth actions

angular ngrx store call multi http service into one effect and dispacth actions

您好,我需要在并行模式下调用多个 http 服务,为此我使用了 forkJoin,但是当它完成时它没有调度操作。

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]])
            );
         }),
      ));

有人可以帮助我吗?

你说这个吗?

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]];
         })
      ));

无效! :(

尝试在每个 http 请求的每个映射之后添加第一个操作符。 forkJoin 在所有 Observables 结束时发出。并且当您执行 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]])
            );
         }),
      ));