2017-06-16 69 views
0
<parent-component> 
<child-component></child-component> 
<child-component></child-component> 
<child-component></child-component> 
<child-component></child-component> 
<parent-component> 

@Component({ 
    selector: 'child-component', 
    inputs: [count] 
}) 

我從父組件調用組件,並且我想打印這些子組件調用的次數。如何從父組件檢查子組件被調用的次數

回答

1

您可以使用屬性構建服務來跟蹤計數。然後在子組件的ngOnInit中增加它。

0

讓用這種方式嘗試

家長component.component.ts

class ParentComponent { 
    @ViewChildren(ChildComponent) childComponents: QueryList<ChildComponent>; 

    ngAfterViewInit() { 
     console.log(this.childComponents.length) 
    } 
} 
+0

@ViewChildren? –

+0

@MithunSudheendran是的,你可以參考這個文檔https://angular.io/api/core/ViewChildren –

相關問題