Why entryComponents is not necessary anymore in Angular 9/ivy compiler?
Asked Answered
R

2

46

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

Raymer answered 1/5, 2020 at 11:45 Comment(0)
H
60

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.

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.

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

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

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

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.

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.

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

See also:

Herzog answered 1/5, 2020 at 18:30 Comment(4)
What about web components / Angular elements? Do I keep it if I want to export my components as custom elements? The docs still says to add them in the entry componentsTwopenny
@Twopenny No. I think you don't need to register elements with entryComponents in NgModules.Herzog
It's look like we have to register this to entry componentsNitriding
Can i remove entry components array without using Ivy with Angular 10?Hi
E
1

When I tried to use a component ErrorComponent as one of the entryComponents, Angular told me:

  'entryComponents' does not exist in type 'NgModule'

enter image description here

Then I found entryComponents was deprecated: https://angular.io/guide/deprecations.

After that I tried to use ErrorComponent as a REGULAR component in the declarations, and it worked:

enter image description here

Edwards answered 14/2 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.