代码之家  ›  专栏  ›  技术社区  ›  Tuxedo Joe

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

  •  1
  • Tuxedo Joe  · 技术社区  · 7 年前

    我正在尝试从5升级到Angular 6,但出现以下错误:

    类型“Observable”上不存在属性“timeout”。

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

    我错过了什么?

    3 回复  |  直到 7 年前
        1
  •  1
  •   danday74    7 年前

    您应该使用ng update进行更新-请参阅 https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4

    根据此链接,这将自动安装 rxjs-compat

    但是,您可以安装 rxjs兼容 如果需要,请手动。

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

        2
  •  5
  •   SiddAjmera    7 年前

    你得加一个 .pipe 然后使用RxJS6中的任何操作符。

    将实现更改为:

    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()
          );
      }
    }
    

    我冒昧地改变了你的想法 AppService. 引用我自己的实现,因为您没有共享 AppService 代码。

        3
  •  1
  •   firegloves    7 年前

    而不是现在 timeout 你应该使用 debounceTime 例如, 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)),
        );
    
        4
  •  1
  •   Moussa Issoufou    6 年前

    你必须执行命令

    npm install rxjs-compat
    
    推荐文章