【问题标题】:Angular2: Implement Rx.Observable.retry() functionalityAngular2:实现 Rx.Observable.retry() 功能
【发布时间】:2016-11-01 17:38:36
【问题描述】:

我已经开始学习 Angular 2。尝试在我的互联网关闭或服务器关闭时实现 retry() 功能。目前,当我的互联网中断时,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 但是,不知道如何在我的情况下实施。帮帮我。

【问题讨论】:

  • 您不要在代码中的任何地方使用重试。
  • 是的,我不知道在哪里使用。我在 component.ts 中尝试过,但出现错误。所以,我删除了它。
  • 这是一个 Observable 操作符。所以你在 Observable 上调用它。在 get() 之后,或者在 map() 之后,或者在 loadItems() 调用之后。

标签: angular rxjs observable


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

read more here...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2019-06-12
    • 2011-12-05
    • 2021-12-12
    • 2014-10-22
    相关资源
    最近更新 更多