简体   繁体   中英

How do I get an event from one parent component to a child-component in angular 8?

I want to get an event that is fired relatively on the top of the dom-hierarchy to a child component further down the hierarchy.The child is not a direct child of the parent

I understand that I do that with the input-decorator. Please note doing it the other way around like it should be is not possible.

The html of the parent looks like this

<element [data]="viewData" (anext)=OnNext($event)></element>

I need to get the $event available in the child. How do I achieve this?

You can create a service with an rxjs Subject, as shown in the psuedo code below.

//Evservice.ts

public subject = new Subject();
OnNext(event) {
   subject.next(event)
}

//childComponent.ts
constructor(ser: Evservice)

ser.subject.subscribe((ev) => {//access your event here});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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