简体   繁体   English

从RXJS 5迁移到6 - IntervalObservable

[英]Migration from RXJS 5 to 6 - IntervalObservable

I updated from RXJS 5.x to RXJS 6.2.2 and have a problem with solving a migration error. 我从RXJS 5.x更新到RXJS 6.2.2并遇到解决迁移错误的问题。

Are there no more IntervalObservables in RXJS 6? RXJS 6中是否没有更多的IntervalObservable? I used IntervalObservable in the following angular component 我在以下角度组件中使用了IntervalObservable

import {Component, OnInit} from '@angular/core';
import {IntervalObservable} from 'rxjs/observable/IntervalObservable';

@Component({
  selector: 'app-date-time-display',
  templateUrl: './date-time-display.component.html',
  styleUrls: ['./date-time-display.component.css']
})
export class DateTimeDisplayComponent implements OnInit {

  constructor() {
  }

  today = Date.now();

  ngOnInit() {
    IntervalObservable.create(1000)
    // .takeWhile(() => this.alive) // only fires when component is alive
      .subscribe(() => {
        this.today = Date.now();
      });
  }
}

When i run either 'ng serve' or 'ng build' I get the following error: 当我运行'ng serve'或'ng build'时,我收到以下错误:

Module not found: Error: Can't resolve 'rxjs/observable/IntervalObservable' in 'C:\Users\Daniel\Documents\IMA\Porsche_lack\git\webapp\porsche-lack-tracking\src\app\date-time-display'
i 「wdm」: Failed to compile.
ERROR in node_modules/rxjs/observable/IntervalObservable.d.ts(1,15): error TS2307: Cannot find module 'rxjs-compat/observable/IntervalObservable'.

FYI: I ran the command rxjs-tslint auto update rules before and it did not find any migration problems! 仅供参考:我之前运行了rxjs-tslint auto update rules命令rxjs-tslint auto update rules ,它没有发现任何迁移问题!

import { interval } from 'rxjs';
import { takeWhile } from 'rxjs/operators';

ngOnInit() {
    interval(1000).pipe(
       takeWhile(() => this.alive)
    .subscribe(() => {
        this.today = Date.now();
    });

rxjs 6 interval rxjs 6间隔

You could do this way: 你可以这样做:

import { Observable } from 'rxjs';

Observable.interval(1000).subscribe(() =>
  // code goes here
);

You can use rxjs interval as of angular 8. 您可以使用角度为8的rxjs间隔。

  import { interval, Observable } from 'rxjs';

  ngOnInit() {
    const TEN_MINUTES = 600000;

    interval(TEN_MINUTES).subscribe(() => {
      this.refreshSession();
    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM