[英]Property 'finally' does not exist on type 'Observable<Object>'
[英]Property 'timeout' does not exist on type 'Observable<Object>'
我正在尝试从 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();
}
}
我在这里缺少什么?
自 Rxjs 6 起,您必须添加一个.pipe
,然后在其中使用任何运算符。
像这样改变你的实现:
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
代码。
现在,您应该使用debounceTime
而不是timeout
。 例如,来自官方文档:
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)),
);
您应该使用 ng update 进行更新 - 请参阅https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4 (向下滚动一点)
根据此链接,这将自动安装rxjs-compat
,它将启用对 RxJs v5 和 v6 的支持
但是,如果需要,您可以手动安装rxjs-compat
。
这将简化 ng5 到 ng6 的转换,然后(可选)稍后在更专注于 RxJ 的任务中将其拉出。
你必须执行命令
npm install rxjs-compat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.