Angular (TS)尝试使用ViewChild访问子组件时出现未使用标签错误

Angular (TS)尝试使用ViewChild访问子组件时出现未使用标签错误,angular,Angular,我在尝试访问父组件中的子组件时遇到问题。我导入了组件并尝试在nginit上声明ViewChild方法。但当我指定变量名时,它会显示unused label。对不起,我是一个有棱角的初学者 下面是您可以看到错误的代码段。我需要另一个函数中的userListComponent通过调用其函数来重新初始化该组件。但由于这个错误,我无法访问该组件 您需要声明 @ViewChild(UserListComponent) userListComponent: UserListComponent 在ngOnI

我在尝试访问父组件中的子组件时遇到问题。我导入了组件并尝试在nginit上声明ViewChild方法。但当我指定变量名时,它会显示unused label。对不起,我是一个有棱角的初学者

下面是您可以看到错误的代码段。我需要另一个函数中的userListComponent通过调用其函数来重新初始化该组件。但由于这个错误,我无法访问该组件


您需要声明

@ViewChild(UserListComponent) userListComponent: UserListComponent
在ngOnInit功能之外。您的
userListComponent
将在
ngAfterViewInit
生命周期事件中初始化

export class TemplateCategoryNewComponent implements OnInit, AfterViewInit {

   @ViewChild(UserListComponent) userListComponent: UserListComponent

   ngOnInit() { ... }

   ngAfterViewInit() {
      // here `userListComponent` is available
   }

}
看这张照片。当您定义userListComponent字段时,此类指令在类的主体中使用,而不是在ngOnInit方法中使用。那就走吧

@ViewChild(UserListComponent) userListComponent: UserListComponent
在恩戈尼特外面