【问题标题】:Property 'interval' does not exist on type 'typeof Observable'“typeof Observable”类型上不存在属性“interval”
【发布时间】:2020-03-24 10:57:18
【问题描述】:

我想从 Angular 9/RxJs 汇集到休息服务,每 5 秒返回一个状态。 rest 服务是一个典型的 get,只是从事务中返回状态。下面的代码基于一些搜索,包括 StackOver 流程​​中的一些答案。我不断收到错误粘贴在下面。

到目前为止我找到的所有答案都告诉我导入我已经导入的内容。

任何额外的检查将不胜感激。

错误:

core.js:6185 ERROR TypeError: rxjs__WEBPACK_IMPORTED_MODULE_2__.Observable.interval is not a function
    at AppComponent.ngOnInit (app.component.ts:37)
    at callHook (core.js:4686)
    at callHooks (core.js:4650)
    at executeInitAndCheckHooks (core.js:4591)
    at refreshView (core.js:11814)
    at renderComponentOrTemplate (core.js:11922)
    at tickRootContext (core.js:13391)
    at detectChangesInRootView (core.js:13425)
    at RootViewRef.detectChanges (core.js:15103)
    at ApplicationRef.tick (core.js:42712)

app.component.ts

//most relevant imports for this question
import { Observable, timer, interval, of } from 'rxjs';
import { concatMap, map, tap, switchMap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  providers: [SseService],
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  //polledTransacaoStatus$: Observable<string>;
  pollingData: any;

  constructor(private http: HttpClient) { }

  ngOnInit() {

    const status$ = this.http.get('http://localhost:8080/extrato/1');

    this.pollingData = Observable.interval(5000) //the issue is here
      .switchMap(() => status$)
      .subscribe(
        data => {
          console.log(data);
        },
        error => {
          console.log(error);
        }
      );
  }

  ngOnDestroy() {
    this.pollingData.unsubscribe();
  }
}

【问题讨论】:

    标签: angular webpack rxjs observable


    【解决方案1】:

    您似乎正在使用旧的(不推荐使用的)语法来设置可观察对象。

    改用这个:

    ngOnInit() {
      const status$ = this.http.get('http://localhost:8080/extrato/1');
    
      this.pollingData = interval(5000)
        .pipe(switchMap((_: number) => status$))
        .subscribe(
          (data: any) => console.log(data),
          (error: any) => console.log(error)
        );
    }
    

    [更新]:在 60 秒后停止

    this.pollingData = interval(5000)
      .pipe(
        switchMap((_: number) => status$),
        takeUntil(timer(60000)), // <= takeUntil will unsubscribe when timer emits  
      ).subscribe(
        (data: any) => console.log(data),
        (error: any) => console.log(error)
      );
    

    【讨论】:

    • Obrigado(谢谢)!你回答了我的问题!如果您不介意最后两个 cmets:(1)如何在 60 秒后停止/取消订阅? (2) 作为一个典型的经验法则,当您只想从事务中获取状态时,您认为使用 Observable.interval 是一种好方法吗?我的意思是,会有一个帖子开始一个事务,预计从 Angular 到 BackEnd 通常需要大约 30 秒,然后上述轮询每 5 秒“询问”一次状态。你会认为除了 RxJs Observable.interval 之外的想法在这种情况下更受欢迎吗?
    • (1) 更新了我的答案以显示如何退订; (2) 更好的方法是使用 Web 套接字(完成后后端会通知前端)。但根据您展示此组件的频率,当前的方法是可以接受的。
    • 好点。我想念一个专注于架构讨论的论坛。我可以在某个地方讨论什么更好,“实施 A”与“实施 B”。如果我在这里发布一个问题,例如“使用‘带有后端通知的 websocket’和‘带有间隔池的 sse’来更新事务请求的 Angular,有哪些赞成/反对?”我肯定会被关闭。当我询问更好的方法时,我想到了从前端进行池化。我相信 websocket 不应该用于汇集交易状态。您可以使用两种方式推送趋于无限的状态(例如聊天对话框)
    • 嗯,从前端拉,我认为你的方法是好的。我在问题跟踪器应用程序项目中构建了类似的东西,我希望将 cmets 更新给屏幕上该问题处于活动状态的任何人。缺点是如果您需要这种方法进行扩展,则必须实现某种服务器端缓存。在没有任何缓存方案和单个服务器的情况下,当您最多有十分之一的用户对同一问题进行可视化和评论时,它不会降低任何性能(我在这里考虑的是每次数据检索时的数据库延迟)。
    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-11-25
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多