简体   繁体   English

如何在 Angular 的不同子组件中订阅 Observable

[英]How to Subscribe Observable in different Child component in Angular

I have component parent and two child A and B component.我有组件parent组件和两个子组件AB I created a Observable in parent and subscribe in A by passing though @input in A .我在parent创建了一个Observable并通过在A传递@input来订阅A

On change on parent A function get calls through subscription.parent A函数上更改时通过订阅获取调用。

But i want to change the B also.但我也想改变B i have checked links我检查了链接

I want to get the change of parent model in all the childrens我想改变所有childrensparent模型

I have solved :我已经解决了:

 let c: Observable<Array<WorkFlow>> = Observable.create((obs: Observer<Array<WorkFlow>>) => {
            this._wfService.getWorkFlows(0, 50).subscribe((data: Array<WorkFlow>) => {
                this.workFlows = data;
                obs.next(this.workFlows);
            }, (err: Response) => {
                console.error(err);
            });
        });
this.workFlowsOb = c.shareReplay(1);

I was returning the c before , now i used c.shareReplay and share to Observable that is shared to all children components我之前返回c ,现在我使用c.shareReplay并共享到 Observable 共享给所有子组件

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

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