使用AOT抛出无法查找组件工厂,在延迟加载的模块中动态渲染组件

Flo*_* Gl 6 javascript lazy-loading angular angular5

目前的行为

我将这些动态组件声明为模块中的入口组件,我也想在其中呈现它们.使用JIT它可以正常工作.以下结构有我的应用程序的一部分,我想渲染它们:app -> home (lazy) -> contracts (lazy) -> search.

所以我将这些组件添加到我用于的模块中search component/route.当我使用AOT进行编译时,每次访问搜索路径时,该应用程序都会告诉我没有组件工厂.当然我搜索谷歌并找到了一些结果:

我尝试将它们添加到ANALYZE_FOR_ENTRY_COMPONENTS提供程序中,我尝试.forRoot()在我的app.module中导入ModuleWithProviders,我也尝试在根模块(app.module)中导入和声明我的动态及其所有依赖组件.一切都导致了同样的错误.

我声明我的动态组件作为入口组件,如:所以

@NgModule({
  imports: [SharedComponentsModule, FinoSchemaFormsModule, TabGroupModule, FinoInputModule],
  declarations: [EnergySearchSettingsComponent, DslSearchSettingsComponent, MobileSearchSettingsComponent, ComparisonDetailSectionComponent],
  entryComponents: [EnergySearchSettingsComponent, DslSearchSettingsComponent, MobileSearchSettingsComponent],
  exports: [EnergySearchSettingsComponent, DslSearchSettingsComponent, MobileSearchSettingsComponent, ComparisonDetailSectionComponent],
  providers: [CategoryMappingProvider]
})
export class ComparisonComponentsModule { }
Run Code Online (Sandbox Code Playgroud)

This module gets imported in the SearchModuleSearchComponent的声明.在这个组件中,我想使用ComponentFactoryResolverI注入动态渲染这些组件SearchComponent.

ngOnInit() {
    this.searchSettingsComponent = this.comparisonService.getSearchComponent(); // returns either EnergySearchSettingsComponent, DslSearchSettingsComponent or MobileSearchSettingsComponent
    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(searchSettingsComponent);
    this.searchSettingsComponent = this.searchContainer.createComponent(componentFactory);
    this.searchSettingsComponent.instance.comparisonSettings = comparisonSettings;
    this.searchSettingsComponent.instance.onSave.subscribe(settings => this.saveSearchSettings(settings));
}
Run Code Online (Sandbox Code Playgroud)

SearchComponent是路由的路由组件search,它是我的contract路由的子路由,它被延迟加载.这又是我的家乡路线的子路线(也是懒人装),这属于主要路线.

环境

Angular version: 5.2.4

For Tooling issues:
- Node version: 8.11.3
- Platform:  Mac
Run Code Online (Sandbox Code Playgroud)

Sun*_*ngh 3

这一定很简单。只需创建SharedModule并将所有可重用动态组件放入其中,从 SharedModule 导出这些组件并将其导入Module所有需要的组件中。延迟加载模块。

由于它是直接导入到模块中的,因此在创建动态组件时它必须可用。