使用 Angular 4 和 Http observables 进行无限轮询
Infinite polling with Angular 4 and Http observables
我正在尝试在我的 Http 服务中构建一个无限轮询,因为我正在构建一个仪表板之王,它调查来自伺服器的数据,这是我几乎可以工作的代码(在我的控制台中,我看到 Json 即将到来,但它并没有反映我的观点......我想注入我的用户:Observable
const usersURL = 'http://my.super.servor.php'
@Injectable()
export class UserService {
users: Observable<User[]>
constructor (public http:Http) {
this.users = http.get(usersURL)
genre mobile ou autre
.map(res => [res.json()]);
let i = this.users.subscribe(
usersURL => console.log(usersURL),
() => {}, // Here we catch up errors
() => console.log("completed!") // Here we catch up if its completed
)
// Here's where I'm trying to do the polling every 5 secondes
let tick$ = Observable.interval(5000);
let response$ =
tick$
.flatMap(() => http.get(usersURL))
.map(res => [res.json()]);
let stockPoller = response$.subscribe(res => console.log(res));
}
您只需将轮询可观察对象分配给 this.users
:
this.users = tick$.flatMap(() => http.get(usersURL)).map(res => [res.json()]);
我正在尝试在我的 Http 服务中构建一个无限轮询,因为我正在构建一个仪表板之王,它调查来自伺服器的数据,这是我几乎可以工作的代码(在我的控制台中,我看到 Json 即将到来,但它并没有反映我的观点......我想注入我的用户:Observable
const usersURL = 'http://my.super.servor.php'
@Injectable()
export class UserService {
users: Observable<User[]>
constructor (public http:Http) {
this.users = http.get(usersURL)
genre mobile ou autre
.map(res => [res.json()]);
let i = this.users.subscribe(
usersURL => console.log(usersURL),
() => {}, // Here we catch up errors
() => console.log("completed!") // Here we catch up if its completed
)
// Here's where I'm trying to do the polling every 5 secondes
let tick$ = Observable.interval(5000);
let response$ =
tick$
.flatMap(() => http.get(usersURL))
.map(res => [res.json()]);
let stockPoller = response$.subscribe(res => console.log(res));
}
您只需将轮询可观察对象分配给 this.users
:
this.users = tick$.flatMap(() => http.get(usersURL)).map(res => [res.json()]);