1
我是AngularJS(2.0.0-beta.16)的新手。我设法通过GET请求来设置一个从API中提取数据的服务。现在,我如何设置它以每n秒来运行GET请求?我见过的其他帖子说,你可以只使用this._http.get(...).interval(5000).map(...);
,但是当我已经试过了,我得到一个打字稿编译错误:如何在Http Observable上使用“间隔”或类似方法?
Property 'interval' does not exist on type 'Observable'.
难道我作出愚蠢的错误或者是有没有做这更好的模式?
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)在观察响应 – Abiodun
RxJs不来的一切,它可以做出来的角2盒,你可能需要进口的'分别interval'支持。查看此页面[这里](http://stackoverflow.com/questions/34548924/missing-observable-methods-rxjs-5-0-0-beta-0)了解更多详情。 – Ownaginatious
您是否已经尝试从“rxjs/Rx”;'而不是从'rxjs/Observable'中导入'import {Observable}? – rinukkusu