[英]Angular2 Return Inner Observable of Nested Observables
I want to make multiple http call in Angular2 using observables. 我想使用observable在Angular2中进行多次http调用。 Each observable is dependent on the prior observable.
每个可观察量取决于先前的可观察量。 If I want to return the inner observable, so I can subscribe to it in the parent component), how can this be done?
如果我想返回内部observable,那么我可以在父组件中订阅它,怎么能这样做呢?
Here is what I've tried, but I wasn't able to subscribe to the observable in the parent component. 这是我尝试过的,但我无法订阅父组件中的observable。
Child Component: 子组件:
observablesFn(){
observable1().subscribe(data1 => {
observable2().subcribe(data2 => {
//I want to return this observable (before subscription b/c I want to subscribe in the parent component)
return observable3();
})
}
}
Your question is hard to understand since you haven't given much context, but it sounds like you're looking to get the result of observable3()
to return from observablesFn()
. 你的问题是很难理解,因为你没有给太多的上下文,但它听起来就像你希望得到的结果
observable3()
从返回observablesFn()
The existing return
statement is returning from your nested inner anonymous function, not your outermost scope. 现有的
return
语句是从嵌套的内部匿名函数返回的,而不是最外层的作用域。 I think you're looking to do something more along these lines. 我认为你希望在这些方面做更多的事情。
observablesFn(){
return observable1().map(data1 => {
return observable2(data1).map(data2 => {
return observable3(data1, data2);
});
});
}
This will return from observablesFn()
, rather than its nested inner function. 这将从
observablesFn()
返回,而不是它的嵌套内部函数。
It's necessary to use .map
rather than .subscribe
, since it returns an observable, rather than a subscription. 有必要使用
.map
而不是.subscribe
,因为它返回一个可观察的,而不是订阅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.