Pass callback to angular component create with route

520 views Asked by At

I have a component that is created with a route and this component has an Output EventEmitter. How can I subscribe to this Output if the component was created on routing?

The component is created like this:

{ path: 'component-A',
  component: ComponentAComponent
}
2

There are 2 answers

2
Ahmed ElMetwally On
<new-component (event_emitter_variable_name)="custom_function()"></new-component>

To know more go to this example


Update

You can create service to can communicate between two components. component 2 will subscribe on a event in the service. component 1 will push to this event in the service. example.

enter image description here

0
Benoît Plâtre On

You can listen to the activate EventEmitter from the router-outlet

<router-outlet (activate)="onRouterActivate($event)"></router-outlet>

$event is your routed Component, then you can listen to an Event. Depending on what you want to achieve, using a Service may be a better choice.