Angular利用@ViewChild在父組件執行子組件的方法

代碼以下: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();
    }
}
相關文章
相關標籤/搜索