我用可观察的东西把头撞在墙上。我能找到的几乎所有文档都采用较旧的 rxjs
语法。
我有一个可观察的 API 调用。我在别处调用它并订阅它 - 试图用来自这个 GET
请求的数据填充一个表。
如果我只是 console.log
我的 getData
函数,它会记录订阅而不是我的数据。 I can successfully console.log data
within the .subscribe
function, but I want to use data
outside of .subscribe()
.
如何从 data
.subscribe()
并在别处使用?或者,我的所有逻辑都必须包含在 .subscribe()
函数中才能使用 data
吗?
getData2() {
return this.m_dbService.get('api/myApiPath').subscribe(
data => (console.log(data)), //This properly logs my data. How to extract `data` out of here and actually use it?
error => { throw error },
() => console.log("finished")
);
}
workbookInit(args){
var datasource = this.getData2(); // this returns the subscription and doesn't work.
}
原文由 Kyle Vassella 发布,翻译遵循 CC BY-SA 4.0 许可协议
只需从
getData()
返回 HTTP 请求并在workbookInit
函数中订阅它。