gpt4 book ai didi

angular - 类型 'timeout' 上不存在属性 'Observable'
转载 作者:行者123 更新时间:2023-12-02 22:51:09 25 4
gpt4 key购买 nike

我尝试从 Angular 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();
}
}

我在这里缺少什么?

最佳答案

从 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 代码。

关于angular - 类型 'timeout' 上不存在属性 'Observable<Object>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52760890/

25 4 0