@ViewChild中的read参数是什么 [英] What is the read parameter in @ViewChild for
问题描述
需要帮助来理解以下语句中{read:ViewContainerRef}的含义.
Need help to understand meaning of {read: ViewContainerRef} in following statement.
@ViewChild('myname', {read: ViewContainerRef}) target;
推荐答案
可以使用#myname
模板变量与element标签关联各种类型的实例.
There can be several instances of various types associated with the element tag with the #myname
template variable.
每个元素都有一个ElementRef
和ViewContainerRef
(可能是应用于该标签的组件或指令中的其他元素).
For each element there is an ElementRef
and ViewContainerRef
(maybe others from components or directives applied to that tag).
如果元素是组件,则存在组件实例.
If the element is a component, then there is the component instance.
也可以将一个或多个指令应用于元素
There can also be one or several directives applied to the element
使用{read: SomeType}
,您可以使用#myname
模板变量告诉元素应返回哪种类型.
With {read: SomeType}
you tell what type should be returned from the element with the #myname
template variable.
如果不提供read
参数,则@ViewChild()
返回
If you don't provide the read
parameter, @ViewChild()
returns the
-
ElementRef
实例(如果未应用任何组件),或者 - 组件实例(如果有).
- 如果您想获得不同的东西,则需要使用
read
明确指定.
ElementRef
instance if there is no component applied, or the- component instance if there is.
- If you want to get something different you need to explicitly specify using
read
.
另请参见如何选择组件模板中的元素?
这篇关于@ViewChild中的read参数是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!