如果将ViewContainerRef指定为@ViewChildren的读取参数,它将引用哪些组件视图 [英] What components view does ViewContainerRef refer to if specified as read parameter to `@ViewChildren`

查看:46
本文介绍了如果将ViewContainerRef指定为@ViewChildren的读取参数,它将引用哪些组件视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读本文在@ViewChildren装饰器上,并显示有关 read 参数的以下内容:

I'm reading this article on @ViewChildren decorator and it says the following about read parameter:

ViewContainerRef-需要创建时需要此令牌模板或组件动态

ViewContainerRef — You need this token when you need to create templates or components dynamically

@ViewChildren(AlertComponent, { read: ViewContainerRef }) alerts: QueryList<AlertComponent>

我的理解是否正确,因为它返回子 AlertComponent 组件的视图容器引用,以便我可以将内容动态添加到这些组件视图中?

Is my understanding correct that it returns view containers references of child AlertComponent components so I can add content dynamically into these components view?

推荐答案

每当编译器遇到任何元素的 {阅读:ViewContainerRef} 时,它都会在该元素上创建一个视图容器.任何元素都可以充当视图容器.每个组件宿主视图可以具有许多子嵌入视图

Whenever a compiler encounters { read: ViewContainerRef } for any element it creates a view container on this element. Any element can act as a view container. Each component host view can have many child embedded views which are checked before the component view child components/host views.

因此,当您使用以下内容时:

So when you use the following:

@ViewChildren(AlertComponent, { read: ViewContainerRef }) alerts;

您基本上是在说要在元素上创建一个视图容器,该元素是 AlertComponent 组件的宿主元素.此视图容器将容纳当前组件而不是 AComponent 的子视图.如果您在 div 元素上定义了一个视图容器,也没有什么不同.视图容器与 AComponent 没有任何关系.

you're basically saying that you want to create a view container on the element which is the host element for the AlertComponent component. This view container will house child views of the current component, not the AComponent. It's not different if you defined a view container on a div element. The view container is not related in any way to AComponent.

另请参见此答案以获取一些示例.

Also see this answer for some examples.

这篇关于如果将ViewContainerRef指定为@ViewChildren的读取参数,它将引用哪些组件视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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