Angular 6:如何隔一段时间获取http

Angular 6: how to get http at an interval

我似乎无法让这个 为我的代码工作。 这是我的原始代码,可以无间断地工作。但不幸的是,它提出了太多的要求。

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

constructor(private http: HttpClient) {}
  ngOnInit() {
  }
  getWeather() {
    this.response = this.http.get<Weather>(this.serviceUrl );
    this.response.subscribe(
      results => {
        this.weathers = results.properties.periods;
        console.log('this.weather ====> ', this.weathers);
      });
    }
}

但是现在我在添加间隔时出现错误:

ERROR TypeError: Cannot read property 'interval' of undefined

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

constructor(private http: HttpClient) {}
  ngOnInit() {
  }
  getWeather() {
    this.response = this.http.get<Weather>(this.serviceUrl );
    this.response.Observable.interval(60000).subscribe(
      results => {
        this.weathers = results.properties.periods;
        console.log('this.weather ====> ', this.weathers);
      });
    }
}

我使用了下面的代码,它没有给我任何错误,但它仍然进入无限响应,就好像间隔没有效果一样。

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { interval } from 'rxjs';
import { take } from 'rxjs/operators';
import { Subject } from 'rxjs';
takeFourNumbers = interval(50000).pipe(take(4));
constructor(private http: HttpClient) {}
  ngOnInit() {
  }
  getWeather() {
    this.response = this.http.get<Weather>(this.serviceUrl );
    this.response.subscribe(
      results => {
        this.weathers = results.properties.periods;
        console.log('this.weather ====> ', this.weathers);
        this.takeFourNumbers.subscribe(x => console.log('Next: ', x));
      });
    }
}

最近使用 Angular 6+ 时旧解决方案不再有效的地方有什么变化吗?我是 Angular.

的新手

尝试如下:

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

interval(50000).pipe(
  map((x) => { /* your code here */ })
);