Angular 如何从父组件检查子组件被调用的次数 @组成部分({ 选择器:'子组件', 输入:[计数] })
我正在从父组件调用组件,我想打印这些子组件调用的次数。您可以使用一个属性构建一个服务来跟踪计数。然后在子组件的ngOnInit中增加它。让我们用这种方法试试 parent-component.component.tsAngular 如何从父组件检查子组件被调用的次数 @组成部分({ 选择器:'子组件', 输入:[计数] }),angular,typescript,Angular,Typescript,我正在从父组件调用组件,我想打印这些子组件调用的次数。您可以使用一个属性构建一个服务来跟踪计数。然后在子组件的ngOnInit中增加它。让我们用这种方法试试 parent-component.component.ts <parent-component> <child-component></child-component> <child-component></child-component> <child-component&
<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]
})
类父组件{
@ViewChildren(ChildComponent)childComponents:QueryList;
ngAfterViewInit(){
log(this.childComponents.length)
}
}
@ViewChildren???@MithunSudheendran是的,您可以参考此文档
class ParentComponent {
@ViewChildren(ChildComponent) childComponents: QueryList<ChildComponent>;
ngAfterViewInit() {
console.log(this.childComponents.length)
}
}