Subscribing to Dynamic Component EventEmitters

Scenario

We have a dynamic HolaComponent that we created here:

Now we are also adding a custom hola:EventEmitter property to the HolaComponent and we are subscribing to it in order to receive an event that notifies us when the component says Hola! .

Approach

Add the hola:EventEmitter to the HolaComponent .

Subscribe to it in the AppComponent and also add a unsubscribe callback such that the hola:EventEmitter is unsubscribed from when the HolaComponent is destroyed:

const sub:Subscription = this.holaComponent.instance.hola.subscribe(event => console.log(event));this.holaComponent.onDestroy(()=> { sub.unsubscribe(); console.log("Unsubscribing")});
}

Demo

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation