为什么在Angular 9/ivy编译器中不再需要entryComponents? [英] Why entryComponents is not necessary anymore in Angular 9/ivy compiler?

查看:91
本文介绍了为什么在Angular 9/ivy编译器中不再需要entryComponents?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以清楚地说明为什么在IVY编译器中不再需要入口组件API吗?换句话说,内部发生了什么变化,因此Angular突然不需要提要动态创建组件了

Can anyone give a clear explanation of why in the IVY compiler, entry components API Is not necessary anymore?. In other words what was change internally so that Angular suddenly doesn't need a heads up that you are going to create component dynamically

推荐答案

ViewEngine

在Ivy之前, ViewEngine编译器基于NgModule配置和html模板执行整个程序分析,然后根据此全局传递信息生成模块和组件工厂.

ViewEngine

Prior to Ivy, ViewEngine compiler performed the whole program analysis based on NgModule configurations and html template and then produced module and component factories based on this global transitive information.

这意味着,如果模板中没有引用某个组件,并且尚未将其添加到NgModule的 entryComponents 数组中,则该组件将不会编译,您可以不能动态渲染它,因为Angular不知道该组件的工厂.

This means that if you have a component which you’re not referencing in the template and you haven't added it to entryComponents array of NgModule then this component won't be compiled and you can't render it dynamically because Angular doesn't know where to get factory for this component.

添加后,编译器将生成专用工厂,并将该工厂添加到内部HashMap中,以便可以通过 ComponentFactoryResolver 对其进行解析.

Once you added it, the compiler will produce dedicated factory and also add this factory to internal HashMap so that it can be resolved through ComponentFactoryResolver.

Ivy引入了一个全新的 ngtsc编译器,其思维模式是装饰者是编译器.

Ivy introduced a completely new ngtsc compiler which mental model is that the decorator is the compiler.

换句话说,ngtsc的整体体系结构是 TypeScript转换器的集合:用于组件,管道,ngModule等.

In other words, the overall architecture of ngtsc it is a set of TypeScript transformers: for component, pipe, ngModule etc.

这些转换器在适当位置发出诸如 AppComponent.Appfac AppComponent.ɵcmp之类的静态函数,这意味着已转译的代码与原始组件/管道/ngModule所在的文件位于同一文件中位于.因此,我们在同一位置拥有工厂(实例化Angular组件/管道/模块所需的所有代码),并且可以通过这些静态属性轻松访问它们.

These transformers emit static functions like AppComponent.ɵfac, AppComponent.ɵcmp in place meaning that transpiled code resides in the same file where the original component/pipe/ngModule is located. So we have factories(all code required for instantiating Angular components/pipes/modules) in the same place and they can be easily accessed by those static properties.

简而言之,如果TypeScript编译中包含一个文件,该文件的类具有 @Component decorator,则ngtsc编译器将在同一文件中为该类发出工厂.

In simple words, if there is a file included in TypeScript compilation that has class with a @Componentdecorator then ngtsc compiler will emit factory for this class in the same file.

您可以猜测是否将组件导入到任何文件中,并且Angular可以通过静态属性轻松地找到其工厂.

As you can guess if you import that component in any file and Angular can easily discover its factory through static property.

另请参阅:

设计文档(常春藤):单独编译

这篇关于为什么在Angular 9/ivy编译器中不再需要entryComponents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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