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
如何通过选择器id获取angular中组件类的实例作为键?_Angular_Typescript - Fatal编程技术网

如何通过选择器id获取angular中组件类的实例作为键?

如何通过选择器id获取angular中组件类的实例作为键?,angular,typescript,Angular,Typescript,假设我有一个组件,其选择器为child,其类名为ChildComponent 要在AppComponent类中获取ChildComponent类的实例,我可以在app.component.ts文件中从./child/child.component.ts导入{ChildComponent}。但我不想导入实例。我想通过选择器id获取实例 假设AppComponent中的函数getComponentReferenceById(选择器:string)将选择器作为参数并返回ChildComponent类的

假设我有一个组件,其选择器为
child
,其类名为
ChildComponent

要在
AppComponent
类中获取
ChildComponent
类的实例,我可以在
app.component.ts
文件中从./child/child.component.ts导入{ChildComponent}。但我不想导入实例。我想通过选择器id获取实例


假设
AppComponent
中的函数
getComponentReferenceById(选择器:string)
将选择器作为参数并返回
ChildComponent
类的实例。有没有办法在angular中执行此操作?

如果您已经知道您的AppComponent中有(或将有)ChildComponet的实例,您可以使用@ViewChild访问ChildComponet的实例、属性和方法: