Angular2:实现Rx.Observable.retry()功能

时间:2016-11-01 08:31:21

标签: angular rxjs observable

我已经开始学习角度2.当我的互联网停机或服务器关闭时,尝试实现重试()功能。目前,当我的互联网停机时,API调用失败,加载程序无限期运行。所以,我正在尝试实现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 但是,在我的案例中,我不知道如何实施。帮助我。

1 个答案:

答案 0 :(得分:0)

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

<强> read more here...