属性 'timeout' 在类型 'Observable<Object>' 上不存在

Property 'timeout' does not exist on type 'Observable<Object>'

我正在尝试从 5 升级到 Angular 6,但遇到此错误:

ERROR in src/app/services/http.service.ts(17,14): error TS2339: Property 'timeout' does not exist on type 'Observable'.

我的代码在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();
    }
}

我在这里错过了什么?

现在您应该使用 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)),
    );

您必须添加一个 .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代码。

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

根据此 link,这将自动安装 rxjs-compat,这将启用对 RxJs v5 和 v6 的支持

不过,您可以根据需要手动安装 rxjs-compat

这将简化 ng5 到 ng6 的过渡,然后(可选)稍后在更专注于 RxJs 的任务中将其退出。

你必须执行命令

npm install rxjs-compat