2016-11-01 2 views
0

각도 2 학습을 시작했습니다. 내 인터넷이 다운되었거나 서버가 다운되었을 때 retry() 기능을 구현하려고합니다. 현재 내 인터넷이 다운되면 API 호출이 실패하고 로더가 무기한 실행됩니다. 그래서 retry() 기능을 구현하려고합니다.Angular2 : Rx.Observable.retry() 기능 구현

Component.ts :

import { Component } from '@angular/core'; 
import { ItemService } from './item.service'; 
@Component({ 
    selector: 'itemspage', 
    templateUrl: 'items.component.html', 
    providers: [ItemService] 
}) 
export class ItemComponent implements OnInit { 
    constructor(private itemService: ItemService) { } 
    items: string[] = []; 
    loader: boolean = false; 
    ngOnInit() { 
     this.loader = true; 
     this.itemService.loadItems() 
     .subscribe((resp) => { 
      this.items = resp.data; 
      this.loader = false; 
     }); 
    } 
} 

Service.ts는 :

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 

import 'rxjs/add/operator/map'; 

@Injectable() 
export class ItemService { 

    constructor (private http: Http) {} 

    loadItems() { 
    return this.http.get('localhost:8080/****') 
     .map(res => res.json()); 
    } 
} 

나는이 기사를 발견 : https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/retry.md이 하지만, 내 경우에는 구현하는 방법을 모르겠어요. 도와주세요.

+0

당신은 당신의 코드에서 어디 재 시도 사용하지 마십시오. – micronyks

+0

그래, 어디에서 사용해야할지 모르겠다. component.ts에서 시도했지만 오류가 발생했습니다. 그래서 나는 그것을 제거했다. – NNR

+0

Observable 연산자입니다. Observable에서 호출합니다. get() 후 또는 map() 후 또는 loadItems() 호출 후. –

답변

0
loadItems() { 
    return this.http.get('localhost:8080/****') 
    .retry(5)         //<<<@@@@@ use it here 
    .map(res => res.json()); 
} 

read more here...