【问题标题】:angular ngrx store call multi http service into one effect and dispacth actionsangular ngrx store 将多个 http 服务调用为一个效果和分发操作
【发布时间】:2020-12-08 00:35:13
【问题描述】:

您好,我需要在并行模式下调用多 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]])
            );
         }),
      ));

有人可以帮我吗?

【问题讨论】:

  • 尝试在每个 http 请求的每个映射之后添加第一个操作符。我认为 forkJoin 会在所有 Obs 结束时发出。并且当您执行 http 请求时,可观察的技术不会结束。所以第一个运营商让这些结束
  • 我在每个 http 调用一个地图!我不明白你
  • 更改 map(response => addClienteSuccess({ response })),for map(response => addClienteSuccess({ response }))、first() 和 map(response => addListinoSuccess({ response })), for map(response => addListinoSuccess({ response })), first(), 并在文件顶部添加 import {first} from 'rxjs/operators'

标签: angular ngrx-store ngrx-effects fork-join


【解决方案1】:

尝试在每个 http 请求的每个映射之后添加第一个操作符。当所有 Observables 结束时,forkJoin 会发出。当你做一个 http 请求时,从技术上讲,observables 并没有结束。所以第一个操作员完成了这些操作。

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

【讨论】:

    【解决方案2】:

    你是这么说的吗?

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

    不工作! :(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-08
      • 2021-05-28
      • 2018-11-05
      • 2022-01-06
      • 2017-12-30
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      相关资源
      最近更新 更多