2017-05-25 128 views
4

我剛剛開始瞭解@ ngrx/store和@ ngrx.effects,並在我的Angular/Ionic應用程序中創建了我的第一個效果。它第一次運行正常,但如果我再次將事件發送到商店(即再次單擊該按鈕),則什麼都不會發生(沒有網絡調用,控制檯日誌中沒有任何內容)。有什麼明顯的我做錯了嗎?這裏的效果:@ngrx效果不會第二次運行

@Effect() event_response$ = this.action$ 
    .ofType(SEND_EVENT_RESPONSE_ACTION) 
    .map(toPayload) 
    .switchMap((payload) => this.myService.eventResponse(payload.eventId,payload.response)) 
    .map(data => new SentEventResponseAction(data)) 
    .catch((error) => Observable.of(new ErrorOccurredAction(error))); 

感謝

回答

11

這聽起來像一個錯誤發生。在這種情況下,catch返回的觀察值中的動作將被放入效果的流中,然後效果將完成 - 這將防止在發出錯誤操作後效果運行。

移動mapcatchswitchMap

@Effect() event_response$ = this.action$ 
    .ofType(SEND_EVENT_RESPONSE_ACTION) 
    .map(toPayload) 
    .switchMap((payload) => this.myService 
    .eventResponse(payload.eventId, payload.response) 
    .map(data => new SentEventResponseAction(data)) 
    .catch((error) => Observable.of(new ErrorOccurredAction(error))) 
); 

構成switchMapcatch將防止如果發生錯誤,在完成的效果。

+0

這是否意味着使用Angular的全局錯誤處理程序將無法使用效果? https://stackoverflow.com/questions/47896171/is-it-possible-to-throw-errors-inside-ngrx-effects-without-completing-the-observ –

+0

謝謝!這對我有效 – Divya

相關問題