0

This is my sample - https://stackblitz.com/edit/angular-router-basic-example-xhr5ds?file=app/app.component.ts

i want take instace of logincomponent which has been mapped to app.component.ts file using router outlet.

How can i able to get that component instace i have tried like below:

import {
  Component,
  ViewChild,
  ViewChildren,
  ContentChild,
  QueryList
} from "@angular/core";
import { LoginViewComponent } from "./views/login/login.component";

@Component({
  selector: "my-app",
  template: `
    <ejs-sidebar
      id="default-sidebar"
      #sidebar
      [target]="target"
      [width]="width"
    >
      <div>Sidebar</div>
    </ejs-sidebar>
    <div class="main-content">
      <div>
        <router-outlet></router-outlet>
      </div>
    </div>
  `,
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  @ViewChild(LoginViewComponent, { static: false })
  DashboardComponentInstance: LoginViewComponent;
  @ViewChildren(LoginViewComponent) helloViewChildren: QueryList<
    LoginViewComponent
  >;
  @ContentChild(LoginViewComponent, { static: true })
  helloContentChild: LoginViewComponent;
  public width: string = "230px";
  public target: string = ".main-content";

  ngAfterViewInit() {
    console.log(this.DashboardComponentInstance);
    console.log(this.helloViewChildren);
    console.log(this.helloContentChild);
    var dashboardInstance = this.DashboardComponentInstance;
  }
}
Kumaresan Sd
  • 1,399
  • 4
  • 16
  • 34
  • 1
    Does this answer your question? [Angular: How to get component instance of router-outlet](https://stackoverflow.com/questions/45587507/angular-how-to-get-component-instance-of-router-outlet) – Ivan Jan 25 '20 at 10:02
  • Hi @Ivan, thanks that works fine for me.. – Kumaresan Sd Jan 25 '20 at 10:04

0 Answers0