Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何从父组件检查子组件被调用的次数 @组成部分({ 选择器:'子组件', 输入:[计数] })_Angular_Typescript - Fatal编程技术网

Angular 如何从父组件检查子组件被调用的次数 @组成部分({ 选择器:'子组件', 输入:[计数] })

Angular 如何从父组件检查子组件被调用的次数 @组成部分({ 选择器:'子组件', 输入:[计数] }),angular,typescript,Angular,Typescript,我正在从父组件调用组件,我想打印这些子组件调用的次数。您可以使用一个属性构建一个服务来跟踪计数。然后在子组件的ngOnInit中增加它。让我们用这种方法试试 parent-component.component.ts <parent-component> <child-component></child-component> <child-component></child-component> <child-component&

我正在从父组件调用组件,我想打印这些子组件调用的次数。

您可以使用一个属性构建一个服务来跟踪计数。然后在子组件的ngOnInit中增加它。

让我们用这种方法试试

parent-component.component.ts

<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)
    }
}