@ViewChild中的read参数是什么 [英] What is the read parameter in @ViewChild for

查看:476
本文介绍了@ViewChild中的read参数是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助来理解以下语句中{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.

每个元素都有一个ElementRefViewContainerRef(可能是应用于该标签的组件或指令中的其他元素).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆