【发布时间】:2016-09-30 03:33:44
【问题描述】:
我对 AngularJS (2.0.0-beta.16) 还很陌生。我已经设法通过 GET 请求设置了一个从 API 中提取数据的服务。现在,如何将其设置为每 n 秒运行一次 GET 请求?我看过其他帖子说你可以只使用this._http.get(...).interval(5000).map(...);,但是当我尝试这样做时,我得到了一个 Typescript 编译错误:
“可观察”类型上不存在属性“间隔”。
我犯了一个愚蠢的错误还是有更好的模式来做这件事?
import { Injectable } from 'angular2/core';
import { Http, Response } from "angular2/http";
import { Observable } from "rxjs/Observable";
import * as _ from "js/lodash.js";
import { Foo } from "./foo";
@Injectable()
export class FooService {
fooList: Observable<Foo[]>;
constructor(private _http: Http) {
this.fooList = this._http.get('http://localhost:9090/api/').map(
response => {
var json = response.json();
if(response.ok === true) {
let newFooList: Foo[] = [];
_.forEach(json, f => {
newFooList.push(new Foo(f));
});
return newFooList;
}
throw Error("Bad status: " + response);
});
}
}
【问题讨论】:
-
在可观察响应上使用 .delay(3000)
-
RxJs 并没有提供它在 Angular 2 中开箱即用的所有功能。您可能需要单独导入对
interval的支持。查看此页面here 了解更多详情。 -
您是否已经尝试导入
import { Observable } from "rxjs/Rx";而不是仅从rxjs/Observable导入?
标签: angularjs http typescript angular rxjs