【发布时间】:2021-12-01 12:20:58
【问题描述】:
在我的 Angular 应用程序中,我有一个带有两个子组件的父组件。我想共享一个 observable,将其命名为 newUploadStarted$,它在父组件中创建,两个子组件在其上进行通信。这两个子组件是同一组件的多个实例,如果单击它们,则会触发文件上传。我想在子组件中的 newUploadStarted$ 上引发一个事件,以便所有子事件通过清除它们可能拥有的旧消息来对其做出反应。
export class ReportUploaderComponent implements OnInit {
@Input() newUploadStarted$: Observable<1>;
...
onChooseBlueReportFile(files: FileList) {
newUploadStarted$.raiseEvent(1); // Specific number isn't as important as letting the child raise the event on something it did not create.
}
对我来说,这应该是一件非常简单的事情,但我只是没有看到一个对我有意义的例子。我错过了什么?
谢谢, 樵夫
【问题讨论】:
-
您可能想要使用
Subject而不是Observable。相关的区别是前者可以多播给许多观察者。在使用Subject时,您可以在子组件中使用.next(...)来引发事件。 -
@miqh 您能否将该回复粘贴为答案,以便我为您提供信用?我没有意识到 Observable 本质上是一个两方队列。然而,即使我有一个监听器,如何使用 Observable 引发事件也不是很明显。
标签: node.js angular rxjs observable