You need to provide the component and enter it inside the component constructor, where you need to call ngOnInit of another component, like me.
Plunker demo: https://plnkr.co/edit/M0d65wHjfg4KfwaQ5mPM?p=preview
//our root app component import {Component, NgModule, VERSION, OnInit} from '@angular/core' import {BrowserModule} from '@angular/platform-browser' @Component({ selector: 'my-app', template: ` <div> <h2>Hello {{name}}</h2> <comp-one></comp-one> <comp-two></comp-two> </div> `, }) export class App { name:string; constructor( ) { this.name = `Angular! v${VERSION.full}` } } // ComponentOne with ngOnInit @Component({ selector: 'comp-one', template: `<h2>ComponentOne</h2>`, }) export class ComponentOne implements OnInit { ngOnInit(): void { alert("ComponentOne ngOnInit Called") } } // Added provider of ComponentOne here and injected inside constructor the on button click call ngOnInit of ComponentOne from this component @Component({ providers:[ComponentOne], selector: 'comp-two', template: ` Component Two: <button (click)="callMe()">Call Init of ComponentOne</button>`, }) export class ComponentTwo implements OnInit { constructor(private comp: ComponentOne ) { this.name = `Angular! v${VERSION.full}` } public callMe(compName: any): void { this.comp.ngOnInit(); } } @NgModule({ imports: [ BrowserModule ], declarations: [ App, ComponentOne, ComponentTwo ], bootstrap: [ App ] }) export class AppModule {}
source share