【问题标题】:Property 'timeout' does not exist on type 'Observable<Object>''Observable<Object>' 类型不存在属性 'timeout'
【发布时间】:2019-03-16 14:02:27
【问题描述】:

我正在尝试从 Angular 5 升级到 Angular 6 并收到此错误:

src/app/services/http.service.ts(17,14) 中的错误:错误 TS2339: “可观察”类型上不存在属性“超时”。

我在 http.service.ts 中的代码:

import { throwError as observableThrowError,  Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { environment } from "environments/environment";
import { AppService } from 'app/app.service';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class HttpService {

    private baseUrl = environment.apiUrl;

    constructor(private http: HttpClient, private appService: AppService) { }

    public get(endpoint: string): Observable<any>{
        return this.http.get(this.baseUrl + endpoint)
            .timeout(this.appService.timeoutInterval)
            .retryWhen(error => error.delay(this.appService.waitInterval)
                .take(this.appService.numberOfRetries)
                .concat(observableThrowError(new Error())))
            .share();
    }
}

我在这里错过了什么?

【问题讨论】:

  • 我对我的回答做了一些修改。请看一看。

标签: angular angular5 angular6 rxjs5 rxjs6


【解决方案1】:

现在你应该使用debounceTime 而不是timeout。 例如from official doc:

this.heroes$ = this.searchTerms.pipe(
      // wait 300ms after each keystroke before considering the term
      debounceTime(300),

      // ignore new term if same as previous term
      distinctUntilChanged(),

      // switch to new search observable each time the term changes
      switchMap((term: string) => this.heroService.searchHeroes(term)),
    );

【讨论】:

【解决方案2】:

你必须添加一个.pipe,然后从 Rxjs 6 开始在其中使用你的任何运算符。

像这样改变你的实现:

import { throwError ,  Observable, timer } from 'rxjs';
import { Injectable } from '@angular/core';
import { environment } from "environments/environment";
import { AppService } from 'app/app.service';
import { HttpClient } from '@angular/common/http';

import { 
  timeout,
  retryWhen,
  take,
  concat,
  share,
  delayWhen
} from 'rxjs/operators';

@Injectable()
export class HttpService {

  private baseUrl = environment.apiUrl;

  constructor(
    private http: HttpClient, 
    private appService: AppService
  ) {}

  public get(endpoint: string): Observable < any > {
    return this.http.get(this.baseUrl + endpoint)
      .pipe(
        timeout(2500),
        retryWhen(errors =>
          errors.pipe(
            delayWhen(val => timer(val * 1000))
          )
        ),
        take(2),
        concat(throwError('This is an error!')),
        share()
      );
  }
}

PS:我冒昧地用我自己的实现更改了您的 AppService. 引用,因为您没有分享您的 AppService 代码。

【讨论】:

    【解决方案3】:

    您应该使用 ng update 进行更新 - 请参阅 https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4(向下滚动一点)

    根据此链接,这将自动安装 rxjs-compat,这将支持 RxJs v5 和 v6

    但是,如果需要,您可以手动安装 rxjs-compat

    这将简化从 ng5 到 ng6 的转换,然后(可选)稍后在更专注于 RxJ 的任务中将其拉出。

    【讨论】:

    • 唯一的问题是 rxjs-compat 没有自动安装,你的回答让我意识到
    【解决方案4】:

    你必须执行命令

    npm install rxjs-compat
    

    【讨论】:

    • 请通过解释一下来改进您的答案,例如。您为什么认为这是缺少的部分,或者为什么需要该步骤。
    猜你喜欢
    • 2018-06-25
    • 2020-07-13
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2019-03-16
    • 2018-03-18
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多