代碼以下:app
@Component({ selector: 'my-app', template: ` <step-bar #stepBar></step-bar> ` }) export class AppComponent{ //利用模板變量stepBar獲取子組件的引用 @ViewChild('stepBar') stepBar: StepBarComponent; //執行子組件的init方法(須要在AfterViewInit鉤子後執行) ngAfterViewInit() { this.stepBar.init(); } }