Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rxJS和ngrx-效果内部成功/失败的正确结构是什么?_Rxjs_Ngrx - Fatal编程技术网

rxJS和ngrx-效果内部成功/失败的正确结构是什么?

rxJS和ngrx-效果内部成功/失败的正确结构是什么?,rxjs,ngrx,Rxjs,Ngrx,我在angular 2项目中工作,使用ngrx和rxjs技术 现在我有一个问题: 我试图声明一种效果。 该效果具有http请求,并且仅当它成功时,我想调用其他http请求,因此仅当它也成功时,然后发送一个成功操作 我已经通过抛出一个错误对它进行了测试,但它总是发送动作 见: 您可以将其拆分为多个操作,也可以使用Observable.forkJoin @Effect() createEntity$ = this.actions$.ofType(CREATE_ENTITY) .swit

我在angular 2项目中工作,使用ngrx和rxjs技术

现在我有一个问题:

我试图声明一种效果。 该效果具有http请求,并且仅当它成功时我想调用其他http请求,因此仅当它也成功时,然后发送一个成功操作

我已经通过抛出一个错误对它进行了测试,但它总是发送动作

见:


您可以将其拆分为多个操作,也可以使用
Observable.forkJoin

       @Effect() createEntity$ = this.actions$.ofType(CREATE_ENTITY)
.switchMap((action: CreateEntity) => {
    return Observable.forkJoin(
       this.httpService.callOne(),
       this.httpService.callTwo()
    )
    .catch((error) => Observable.of(new createEntityFailure(error)))
    .map(mappedResponse => ({ type: CREATE_ENTITY_SUCCESS, payload: mappedResponse }))
});
因为forkJoin是并行的,所以它对您不起作用。您只需在第一个API调用上切换映射,然后返回第二个:

       @Effect() createEntity$ = this.actions$.ofType(CREATE_ENTITY)
.switchMap((action: CreateEntity) => {
    return this.httpService.callOne();
 })
 .switchMap((response) => {
    return this.httpService.callTwo()
      .map(secondResponse => ({ 
          type: CREATE_ENTITY_SUCCESS,
          payload: {
              first: response, 
              second: secondResponse
          } 
       }))
 })
    .catch((error) => Observable.of(new createEntityFailure(error)))
});

您可以将其拆分为多个操作,也可以使用
Observable.forkJoin

       @Effect() createEntity$ = this.actions$.ofType(CREATE_ENTITY)
.switchMap((action: CreateEntity) => {
    return Observable.forkJoin(
       this.httpService.callOne(),
       this.httpService.callTwo()
    )
    .catch((error) => Observable.of(new createEntityFailure(error)))
    .map(mappedResponse => ({ type: CREATE_ENTITY_SUCCESS, payload: mappedResponse }))
});
因为forkJoin是并行的,所以它对您不起作用。您只需在第一个API调用上切换映射,然后返回第二个:

       @Effect() createEntity$ = this.actions$.ofType(CREATE_ENTITY)
.switchMap((action: CreateEntity) => {
    return this.httpService.callOne();
 })
 .switchMap((response) => {
    return this.httpService.callTwo()
      .map(secondResponse => ({ 
          type: CREATE_ENTITY_SUCCESS,
          payload: {
              first: response, 
              second: secondResponse
          } 
       }))
 })
    .catch((error) => Observable.of(new createEntityFailure(error)))
});
这个怎么样:

this.actions$
    .ofType(CREATE_ENTITY)
    .map((action: CreateEntity) => action.payload)
    .switchMap(payload =>
      this.httpService.getDefaultEntityData(payload.type)
        .mergeMap(entity => this.httpService.addEntity(entity))
        // .mergeMap(entity => Observable.throw('error')) // or this for testing
        .mergeMap(response => new actions.Action(...))
        .catch(error => new actions.Error(...))
    );
这个怎么样:

this.actions$
    .ofType(CREATE_ENTITY)
    .map((action: CreateEntity) => action.payload)
    .switchMap(payload =>
      this.httpService.getDefaultEntityData(payload.type)
        .mergeMap(entity => this.httpService.addEntity(entity))
        // .mergeMap(entity => Observable.throw('error')) // or this for testing
        .mergeMap(response => new actions.Action(...))
        .catch(error => new actions.Error(...))
    );
1) 如果返回
Observable
,则可能需要
swithMap
而不是
map

2) 始终已调度操作,因为您从
catch
返回非错误
可观察的
。将
Observable.of更改为
Observable.throw
将进一步抛出错误

@Effect()
createEntity$ = this.actions$.ofType(CREATE_ENTITY)
    .switchMap((action: CreateEntity) =>
        this.httpService.getDefaultEntityData(action.payload.type)
    )
    .switchMap((entity) => { //                                     <------ switchMap here
        return  Observable.throw("testing only");
        /*if (entity) {
             entity.title = entity.type;
             return this.httpService.addEntity(entity);
        }*/
    })
    .catch((error) =>
        Observable.throw(new createEntityFailure(error)) //         <------ throw here
    )
    .map((mappedResponse) => 
        ({ type: CREATE_ENTITY_SUCCESS, payload: mappedResponse })
    );
@Effect()
createEntity$=此类型的.actions$(创建实体)
.switchMap((操作:CreateEntity)=>
this.httpService.getDefaultEntityData(action.payload.type)
)
.switchMap((实体)=>{//
Observable.throw(新createEntityFailure(错误))//
({type:CREATE\u ENTITY\u SUCCESS,有效载荷:mappedResponse})
);
1)如果您返回
可观察的
,您可能需要
swithMap
而不是
地图

2) 操作始终已被调度,因为您从
捕获返回非错误
可观察的
。将
可观察的.of
更改为
可观察的.throw
将进一步抛出错误

@Effect()
createEntity$ = this.actions$.ofType(CREATE_ENTITY)
    .switchMap((action: CreateEntity) =>
        this.httpService.getDefaultEntityData(action.payload.type)
    )
    .switchMap((entity) => { //                                     <------ switchMap here
        return  Observable.throw("testing only");
        /*if (entity) {
             entity.title = entity.type;
             return this.httpService.addEntity(entity);
        }*/
    })
    .catch((error) =>
        Observable.throw(new createEntityFailure(error)) //         <------ throw here
    )
    .map((mappedResponse) => 
        ({ type: CREATE_ENTITY_SUCCESS, payload: mappedResponse })
    );
@Effect()
createEntity$=此类型的.actions$(创建实体)
.switchMap((操作:CreateEntity)=>
this.httpService.getDefaultEntityData(action.payload.type)
)
.switchMap((实体)=>{//
Observable.throw(新createEntityFailure(错误))//
({type:CREATE_ENTITY_SUCCESS,payload:mappedResponse})
);

@user2783091编辑响应以包含不同的option@user2783091编辑响应以包含其他选项