简体   繁体   English

如何订阅子组件angular2中的observable?

[英]how to subscribe to observable in child component angular2?

I'm getting an undefined value error when subscribing to an observable in a child component.订阅子组件中的 observable 时出现undefined value错误。 What is the correct way to subscribe to it?订阅它的正确方法是什么?

I tried to use ngOnChanges but I get the store observable there and I don't want to bind the store to the child component我尝试使用 ngOnChanges 但我在那里得到了可观察的商店,我不想将商店绑定到子组件

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

First of all you can pass the result value using the async pipe like this :首先,您可以使用异步管道传递结果值,如下所示:

[data]="vhqEndpointOptions$ | async"

and then you won't have to manage the subscription and just get the array value.然后您将不必管理订阅而只需获取数组值。

if you need the observable you can use a setter for the @Input :如果您需要可观察的,您可以为 @Input 使用 setter:

@Input("data") 
set Data(value:Observable<any[]>){
  if(value){
    value.subscribe(...);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM