Angular Ivy忽略entryComponents设置

时间:2020-04-20 21:18:24

标签: angular angular-ivy

我有一个自定义组件装饰器,用于将组件链接到“名称”,以便使用JSON对象将组件链接到层次结构中。

@MyDecorator('name1')
@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})
export class MyComponent implements OnInit {
  // ... rest of the implementation
}

装饰器的作用是将组件自动注册到Map中,就像下面这样:

{
  "name1": MyComponent
}

我有一个配置,几乎是一个动态路由配置,存储为JSON对象 external 到应用程序代码。

[
  {
    link: '/myroute',
    component: 'name1'
  }
]

在动态主机组件中,我使用如下代码来实例化指定的组件:

// ... gets the component "name"
const componentName = getConfigForRoute('myroute'); 
// should return MyComponent class, if MyComponent gets included in the app bundle:
const componentType = componentRegistry[componentName];

// componentType is ok with ng serve, and undefined in prod builds!
const cf = this.componentFactoryResolver.resolveComponentFactory(componentType);
// reportOutlet is a ViewContainerRef
this.reportOutlet.createComponent(cf);

通过ng serve在开发人员模式下运行时,一切正常。
通常情况下,在生产版本中事情进展并不顺利:用MyDecorator装饰的组件除了应用程序的ts之外没有被NgModule代码引用,因此他们被热情的编译器高兴地丢弃了。

我(和其他所有人一样)曾经将它们包含在模块的EntryComponents数组中,但是现在看来Ivy只是忽略了它,因此我没有明显的选择来确保组件是没有从构建中删除。


@NgModule({
  declarations: [
    AppComponent,
    MyComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    // ...some other stuff
  ],
  entryComponents: [MyComponent],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

如何完成与EntryComponents相同的工作或使其重新工作?也就是说,确保包含MyComponent吗?理想的解决方案是包括全局列表:拥有装饰器的全部目的是使我不需要维护另一个组件列表,但我会接受所有可行的选择。

谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我以一种非常丑陋的方式破解了这个问题。

AFAICT,常春藤让from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH table = document.add_table(rows=1, cols=1) cell = table.rows[0].cells[0] cell_paragraph = cell.paragraphs[0] cell_paragraph.text = 'test' cell_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER 和Webpack在摇树期间放弃课程时让Mojo发挥Mojo的作用(不确定细节,这只是我做的一般照片)。

因此,为了“模拟” entryComponents,我们必须在某处包括组件的“使用”。我不想在模块外引用该组件,所以我用tsc“仿真”解决了这个问题。 它很简单:

entryComponents

我真的希望有更好的方法。
我认为EntryComponents有其用例的一部分,例如我的。