[英]Angular 9 Ivy entryComponents conversion
@Component({
selector: 'dynamic',
template: '<ng-template *ngFor="let portal of portals" [cdkPortalOutlet]="portal"></ng-template>',
// entryComponents before Ivy
entryComponents: [Component1, Component2, Component3]
})
class DynamicComponent<T extends BaseComponentClass>() {
portals: ComponentPortal<any>[] = [];
constructor(@Inject(COMPONENTS_TOKEN) components: T[]) {
// Something like this
this.portals = components.map(c => new ComponentPortal(c));
}
}
@NgModule({
declarations: [
DynamicComponent
Component1,
Component2,
Component3,
],
imports: [PortalModule, CommonModule, MyOtherModule]
})
I have a component something like above that I create dynamically using ComponentPortal
and cdkPortalOutlet
.我有一个类似上面的组件,我使用
ComponentPortal
和cdkPortalOutlet
动态创建。 The DynamicComponent
itself has many outlets and components that it creates. DynamicComponent
本身有许多它创建的出口和组件。 Previously with entryComponents
I would list every component that could be loaded into the DynamicComponents
outlets.以前使用
entryComponents
我会列出可以加载到DynamicComponents
出口的每个组件。 This was working as expected, but now after upgrading to Ivy and removing deprecated entryComponents
, this no longer works.这按预期工作,但现在升级到 Ivy 并删除不推荐使用的
entryComponents
,这不再有效。 I create DynamicComponent
and it initializes fine, but components inside of DynamicComponent
don't seem to be created and cause the entire DynamicComponent
to fail its render.我创建了
DynamicComponent
并且它初始化得很好,但是DynamicComponent
内部的组件似乎没有被创建并导致整个DynamicComponent
无法渲染。 If I remove the outlets in DynamicComponent
then everything else in DynamicComponent
renders fine.如果我删除
DynamicComponent
中的插座,那么DynamicComponent
其他所有内容都可以正常呈现。
My thoughts are that the dependency components of DynamicComponent are not resolved when creating it.我的想法是DynamicComponent的依赖组件在创建时没有解析。 The dependency components are not used in any other angular components.
依赖组件未在任何其他角度组件中使用。 Am I missing something on how to convert old entryComponent behaviour to work with Ivy?
我是否缺少有关如何将旧的 entryComponent 行为转换为与 Ivy 一起使用的信息?
EDIT: "buildOptimizer": false
fixes it but ideally want to keep this on.编辑:
"buildOptimizer": false
修复它,但理想情况下希望保持此状态。
You can create an entryComponents
array in your DynamicComponent
class where you list the components that are not referenced elsewhere, eg:您可以在您的
DynamicComponent
类中创建一个entryComponents
数组,您可以在其中列出其他地方未引用的组件,例如:
class DynamicComponent<T extends BaseComponentClass>() {
entryComponents = [Component1, Component2, Component3];
...
The fact that the components are referenced will prevent them from being tree shaken, even though the variable is not used for anything.组件被引用的事实将防止它们被摇树,即使该变量没有用于任何事情。 See an interesting discussion about this kind of problem and possible solution at this Angular GitHub issue .
在此Angular GitHub 问题 中查看有关此类问题和可能的解决方案的有趣讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.