2017-06-16 72 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 –

相关问题