2017-12-06 6 views
1

HttpClient에서 오류가 발생한 후 코드를 실행하는 방법에 조금 어려움을 겪고 있습니다. http로 실패했을 때 로딩 스피너를 끄는 플래그를 설정하는 방법을 알고 싶습니다. 나는 행복 경로에 5각도 HttpClient 오류 처리

private fetch(): Observable<LegalTerm[]> { 

    this.isLoading = true; 
    return this._http 
       .get<LegalTerm[]>(AppSettings.LegalTermUrl, { withCredentials: true }) 
       .do(() => this.isLoading = false) 
       .catch<any, LegalTerm>(this._trace.handleError) 
       .do(() => this.isLoading = false) 
    } 

그래서 isLoading = false 작품을 RxJs 5.5.2 및 각도를 사용하지만 catch 후 그렇게하는 방법을 잘하고 있습니다. 코드가 지금과 같은 방식으로 작동하지 않습니다. 나는 또한 당신이 완료/오류 이벤트에 false에 플래그를 변경하려면 finally 연산자를 사용할 수있는 각도 문서

public handleError<T>(operation = 'operation', error: any, result?: T) { 
    // do logging etc 
    return Observable.of(result as T); // return empty result 
} 
+0

here를 찾을 수 있습니다. –

+0

'.catch (this._trace.handleError)'는 조만간 당신을 물게 할 코드입니다. 고쳐. –

답변

2

에 따라 사용자 지정 오류 처리기를 사용하고 있습니다. 다음과 같이

은 할 수 : 운영자에 대한

import { BehaviorSubject }from 'rxjs/BehaviorSubject'; 
import 'rxjs/add/operator/finally'; 

@Component() 
export class FooComponent { 
    private loadingSub$ = new BehaviorSubeject<boolean>(false); 
    loading$ = this.loadingSub$.asObservable(); 

    constructor(private fooService: FooService){ 
    } 

    fetch(){ 
    this.loadingSub$.next(true); 
    this.fooService.getData() 
    // notice that getData() returns a stream that isnt infinite 
    .finally(()=>this.loadingSub$.next(false)) 
    .subscribe(fooData => console.log(fooData)); // error handling not considered 
    } 
} 

@Injectable() 
export class FooService { 
    constructor(private http: HttpClient){} 

    getData(): Observable<any>{ 
     // error handling not considered 
     return this.http.get('foourl'); 
    } 
} 

더 많은 정보를 원하시면 당신은`HttpInterceptor`를 사용해야합니다

+0

'import 'rxjs/add/operator/finally'; '내가 필요한 것입니다. 감사합니다! – Anand