ng.getComponent
检索与给定 DOM 元素关联的组件实例。
Retrieves the component instance associated with a given DOM element.
ng.getComponent<T>(element: Element): T | null
参数
element | Element | 要从中检索组件的 DOM 元素。 DOM element from which the component should be retrieved. |
返回值
与元素关联的组件实例;如果没有与之关联的组件,则为 null
T | null: Component instance associated with the element or null if there is no component associated with it.
使用说明
给定以下 DOM 结构:
Given the following DOM structure:
<my-app>
<div>
<child-comp></child-comp>
</div>
</my-app>
在 <child-comp> 上调用 getComponent 将返回与此 DOM 元素关联的 ChildComponent。
Calling getComponent on <child-comp> will return the instance of ChildComponent associated with this DOM element.
在 <my-app> 上调用该函数将返回 MyApp 实例。
Calling the function on <my-app> will return the MyApp instance.