AngularNGRXEffects-执行代码块而无需等待HttpResponse
创始人
2024-10-28 18:00:33
0

在effect中使用RxJS的flatMap操作符等待HttpResponse返回。示例代码如下:

import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Observable, of } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class UserEffects {
  constructor(private actions$: Actions, private http: HttpClient) { }

  @Effect()
  getUser$: Observable = this.actions$.pipe(
    ofType('GET_USER'),
    map((action: any) => action.payload),
    switchMap((id: number) => {
      return this.http.get(`api/user/${id}`).pipe(
        map((res) => {
          return { type: 'USER_LOADED', payload: res };
        }),
        catchError((error) => {
          return of({ type: 'USER_ERROR', payload: error });
        })
      );
    })
  );
}

相关内容

热门资讯

透视系统(wepOke)微扑克... 透视系统(wepOke)微扑克钻石能提高胜率吗(透视)详细辅助黑科技教程准备好在微扑克钻石能提高胜率...
透视新版!wepokerplu... 透视新版!wepokerplus透视脚本免费(透视)切实真的是有挂(详细辅助微扑克教程);支持2-1...
透视app!wpk辅助器下载(... 透视app!wpk辅助器下载(透视)详细辅助AI教程(好像是有挂)1、让任何用户在无需wpk辅助器下...
透视私人局(WEPoke)德扑... 透视私人局(WEPoke)德扑软件决策(透视)详细辅助爆料教程;最新版2024是一款经典耐玩的益智游...
透视规律!wepoker透视脚... 透视规律!wepoker透视脚本免费(透视)起初是真的有挂(详细辅助插件教程);原来确实真的有挂(需...
透视游戏!aapoker辅助工... 透视游戏!aapoker辅助工具存在吗(透视)详细辅助我来教教你(果然存在有挂)1、任何aapoke...
透视ai(wepOkE)微扑克... 透视ai(wepOkE)微扑克辅助钻石(透视)详细辅助细节方法准备好在微扑克辅助钻石ia的高塔上攀登...
透视线上!wepoker游戏安... 透视线上!wepoker游戏安装教程(透视)一向是真的有挂(详细辅助技巧教程)是由北京得wepoke...
透视代打!智星菠萝辅助怎么买(... 透视代打!智星菠萝辅助怎么买(透视)详细辅助系统教程(切实真的是有挂)1、智星菠萝辅助怎么买ai辅助...
透视免费(wepOke)wep... 透视免费(wepOke)wepoke透明挂辅助软件是真的吗(透视)详细辅助2025新版;科技安装教程...