在共享的 Observable 上引发事件

Raise an event on a shared Observable

在我的 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 作为 parent 和 children 组件之间的通信总线。

A Subject 将允许您在所有观察者之间多播任何值。

newUploadStarted$.next();

如果您发现需要在不同 parent 的 children 之间共享同一总线,您可能需要考虑将其重新定位到服务。