0

While using router-outlet, i have used “active” event ton take child component instance.

A router outlet will emit an activate event any time a new component is being instantiated, and a deactivate event when it is being destroyed.

Sample - https://stackblitz.com/edit/angular-router-basic-example-a9jpsd?file=app/app.component.ts

Example code –

App.component.html

<div class="container">
    <router-outlet (activate)="onActivate($event)"></router-outlet>
  </div>

App.component.ts

public onRouterOutletActivate(event : any) {
    console.log(event);
}

But child component not get displayed at initial loading, but element is in DOM.

Any idea about this problem?

Kumaresan Sd
  • 1,399
  • 4
  • 16
  • 34

1 Answers1

0

Return the boolean value for which (activate) is looking for:

public onRouterOutletActivate(event : any) {
    if(event){
      console.log(event);
      return true;
    }else{ return false; }
}

And also just check in app.component.html, if you are calling the same onRouterOutletActivate function on (activate):

(activate)="onRouterOutletActivate($event)"

Hope it works for you now !!!

Swati Gupta
  • 126
  • 3
  • https://stackblitz.com/edit/angular-router-basic-example-vjuzvn?file=app/app.component.ts no luck for me – Kumaresan Sd Jan 27 '20 at 06:16
  • There is some issue with your css because of which you are not able to see your login component calling. Hardcode some message in your login.component template and then your can see it calling clearly. – Swati Gupta Jan 27 '20 at 06:23
  • sorry, i cant understand – Kumaresan Sd Jan 27 '20 at 06:24
  • Go to login.component.ts and replace template with below: template: `Hello
    `,
    – Swati Gupta Jan 27 '20 at 06:26
  • again same issue https://stackblitz.com/edit/angular-router-basic-example-dfnnkm?file=app/views/login/login.component.ts – Kumaresan Sd Jan 27 '20 at 06:53
  • When you do inspect element, login components is present there which means this is not the issue with displaying child component through router-outlet but actually there is some issue with your sidebar which is not letting displayed your child component. – Swati Gupta Jan 27 '20 at 10:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206699/discussion-between-swati-gupta-and-kumaresan-sd). – Swati Gupta Jan 27 '20 at 10:03