【发布时间】:2017-01-20 11:58:39
【问题描述】:
我是 Angular 2 和 Observables 的新手,但我还没有找到一种方法来“监听”接收到流时订阅的更改,我什至不知道这是否可能,或者它是否是正确的方式。
这就是我以前对 Promise 所做的:
// Constructor and more code
// ...
ngOnInit(): void {
this.loading.present();
this.getItems()
.then(() => this.loading.dismiss());
}
getItems(): Promise {
return this.itemService
.getItems()
.then(items => this.items = items);
}
refresh(refresher): void {
this.getItems()
.then(() => refresher.complete());
}
我已经尝试过订阅/观察,但我只是不知道如何:
// Constructor and more code
// ...
ngOnInit(): void {
this.loading.present();
this.getItems()
.subscribe(() => this.loading.dismiss());
}
getItems(): Subscription {
return this.itemService
.getItems()
.subscribe(items => this.items = items);
}
refresh(refresher): void {
this.getItems()
.subscribe(() => refresher.complete());
}
当然,我得到一个编译错误:Property 'subscribe' does not exist on type 'Subscription',关于如何使用 Observables (RxJS) 实现这一点的任何帮助?
【问题讨论】:
标签: javascript angular rxjs observable angular2-http