Anisha
Anisha

Reputation: 45

How to call a function from root component to another component inside <router-outlet>

I have a header component which is injected into app.component.html(as i want it to be fixed). All other components are defined in <router outlet>. Every component has a function showData() that should be called when date(in the header component) changes. How can I achieve this?. Do I have to use ActivatedRout Configurations?

app.component.html

<app-header></app-header> <router-outlet></router-outlet>

Upvotes: 0

Views: 229

Answers (1)

Guerric P
Guerric P

Reputation: 31815

You can use this:

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

activeComponent: any;

onRouterOutletActivate(component: any) {
    this.activeComponent = component;
}

dateChangedEvent() {
    this.activeComponent.showData();
}

Upvotes: 1

Related Questions