【发布时间】:2019-06-22 11:11:17
【问题描述】:
componentA 有 myService getData observable 的订阅者。 componentB 有按钮。我希望按下此按钮后会出现下一个操作:
myService 将调用服务器并获取数据;
componentA 的订阅者将处理这些数据。
我尝试了以下代码:
(我的服务)
getData(): Observable<IData> {
return this.http.get<IData>(apiUrl);
}
(组件A)
ngOnInit() {
this.myService.getData().subscribe(response => console.log(response));
}
(组件B)
public onClick(): void {
this.myService.getData();
}
当我按下 ComponentB 中的按钮时,ComponentA 中的订阅者什么也不做。在ComponentB中按下按钮后如何在ComponentA中激活订阅者?
【问题讨论】:
-
你应该定义两个组件的关系。
-
B 是 A 的孩子