【发布时间】:2019-07-26 16:57:42
【问题描述】:
我想将值(任何数据)从服务返回到组件作为可观察对象。在对可观察对象进行了几次挖掘之后,发现了以下解决方案:
class AppService {
getData(value) {
// do we have any other best way to return value as an observable
return Observer.create((observer) => {
observer.next(value);
});
}
}
class AppComponent implements OnInit {
ngOnInit() {
this.dataService$ = this.appService.getData().subscribe((data) => {
// do some task with data
});
}
}
【问题讨论】:
-
return of(value) -
你已经找到答案了,这里的问题是什么?
标签: angular rxjs observable