2017-10-19 133 views
0

我已經實現了ngrx-store。我試圖在http調用之前打開微調器。並在通話結束後將其關閉。

getInspectionDetails(order) { 
    this.store.dispatch({ type: SPINNER_VISIBLE, payload: true }) //<-- spinner on 
    return this.$http.get(this.url+'api/Inspection/'+order.documentNumber) 
     .map(this.httpHelper.extractData) 
     .catch(this.httpHelper.handleError) 
     .map(payload => ({ type: INSPECT_PURCHASE_ORDER, payload })) 
     .subscribe(action => this.store.dispatch(action))}); 
} 

現在我嘗試使用

this.store.dispatch({ type: SPINNER_VISIBLE, payload: false }) 

關閉微調。基本上這是與假負載相同的調用來關閉微調器。 但我應該把這個放在哪裏?

+0

我想你可以添加。最後(()=> turnOffSpinner) – Vega

回答

1

subscribe方法需要3個參數:

httpRequest.subscribe(
    action => // do something when the value arrives 
    error => // do something when error occurres 
    () => // do something when observable completes 
); 

,或者你可以在觀察到的使用finally方法:

getInspectionDetails(order) { 
    this.store.dispatch({ type: SPINNER_VISIBLE, payload: true }) //<-- spinner on 
    return this.$http.get(this.url+'api/Inspection/'+order.documentNumber) 
     .map(this.httpHelper.extractData) 
     .catch(this.httpHelper.handleError) 
     .finally(() => this.store.dispatch({ type: SPINNER_VISIBLE, payload: false })) 
     .map(payload => ({ type: INSPECT_PURCHASE_ORDER, payload })) 
     .subscribe(action => this.store.dispatch(action)); 
} 

第二種方法可能會更好,因爲完整的回調將不會在錯誤解僱發生。更多關於在這個問題上的區別:

https://github.com/angular/angular/issues/7865

相關問題