且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

视图,主机视图和嵌入式视图之间有什么区别

更新时间:2023-10-23 14:14:46

有关详细信息,请阅读此文章

For in-depth information read this article Working with DOM in Angular: unexpected consequences and optimization techniques

这留下了许多悬而未决的问题,例如:主机视图所指的是 组件所在的元素,并且嵌入式"视图是 是指组件的模板本身?

This leaves many open questions, such as: a Host view is referring to the element that Component resides in, and an Embedded view is referring to the component’s template itself?

组件视图

Angular中的每个组件都表示为带有节点的视图.视图中的大多数节点类似于组件模板结构,并表示DOM节点.例如,您有一个具有a-comp选择器和以下模板的组件A:

Component view

Each component in Angular is represented as a view with nodes. Most nodes in the view resemble component template structure and represent DOM nodes. For example, you have a component A with the a-comp selector and the following template:

<h1>I am header</h1>
<span>I am {{name}}</span>

编译器生成以下视图节点:

The compiler generates the following view nodes:

elementDataNode(h1)
textDataNode(I am header)
elementDataNode(span)
textDataNode(I am + bindings:[ {{name}} ])

还有许多其他类型的节点,例如

There are many other types of nodes like directive data, query data etc.

主机视图是一个视图,其中包含a-comp组件元素的数据和组件类A的数据. Angular编译器为模块的bootstrapentryComponents中定义的每个组件生成主机视图.当调用factory.createComponent时,每个主机视图都负责创建组件视图. componentFactoryResolver返回的工厂是宿主视图工厂.

The host view is a view that contains data for the a-comp component element and the data for the component class A. Angular compiler generates host views for each component defined in bootstrap or entryComponents of a module. And each host view is responsible for creating a component view when you call factory.createComponent. The factories that are returned by the componentFactoryResolver are the host view factories.

嵌入式视图是为ng-template中指​​定的视图节点创建的视图.它就像一个组件视图,但是没有包装器组件元素和诸如注入器之类的组件数据.基本上,它缺少宿主视图中包含的数据.但这仍然是有效的视图,并且在检测过程中会像其他任何视图一样对其进行检查.

The embedded view is a view that is created for the view nodes specified in the ng-template. It's like a component view but it doesn't have a wrapper component element and component data like injector etc. Basically, it lacks the data that is contained in the host view. But it's still a valid view and is checked during detection as any other view.

在手动创建时(通过 createComponent),以及通过其他方式以声明方式创建时 托管组件(父级)?

Is that true for both cases when created manually (via createComponent) as well as when created declaratively via in another hosting component (parent)?

如果在另一个组件模板中指定了一个组件,则不使用主机视图.父组件视图包含通常在主机视图中定义的节点,该父视图负责创建子组件视图.

If a component is specified in the other component template, then the host view is not used. The parent component view contains the nodes that are usually defined in the host view and this parent view is responsible for creating a child component view.

是否也没有模板的指令也是如此 (因此没有视图)?

Is that the case for Directives as well which don’t have a template (thus no view)?

对,指令没有视图,因此不会为指令创建任何视图.

Right, directives don't have a view so no views are created for directives.

这一切在Shadow dom环境中的工作原理(实际上是浏览器 支持组件宿主)还是模拟环境?

And how all this works in a Shadow dom environment (browser actually support a component host) vs an emulated environment?

每个组件都有一个渲染器,并且该渲染器知道是使用仿真DOM还是使用阴影DOM渲染.渲染器由编译器根据组件装饰器描述符的viewEncapsulation参数定义.

There's a renderer associated with each component and that renderer knows whether to use emulated or shadow DOM rendering. The renderer is defined by the compiler based on the viewEncapsulation parameter of the component decorator descriptor.

以下是我建议阅读的一些文章:

Here are some of the articles that I recommend reading:

  • Exploring Angular DOM manipulation techniques using ViewContainerRef
  • Here is why you will not find components inside Angular
  • The mechanics of DOM updates in Angular
  • The mechanics of property bindings update in Angular
  • Everything you need to know about change detection in Angular