TypesAidesAjouterComponent

TypesAidesAjouterComponent

本文介绍了延迟加载模块中的 EntryComponents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从组件加载模态.在 Angular Material 文档中写入了在 entryComponents 中添加模态组件:

I want to load a modal from a component. In Angular Material documentation is write to add the modal component in entryComponents :

这个:

@NgModule({
  imports: [
    CommonModule,
    AidesRoutingModule
  ],
  declarations: [TypesAidesComponent, TypesAidesAjouterComponent],
  entryComponents : [TypesAidesAjouterComponent]
})
export class AidesModule {

}

在 TypesAidesComponent 中,我想用 TypesAidesAjouterComponent 打开对话框:

In TypesAidesComponent I want open dialog with TypesAidesAjouterComponent :

let dialog = this.dialog.open(TypesAidesAjouterComponent);
    dialog.afterClosed().subscribe(res => {
      if(res){
        this.collection.addItem(res);
      }
    });

我在一个组件延迟加载中:

I'm in a component lazyloading :

{
        path: 'types-aides',
        loadChildren: 'app/modules/aides/aides.module#AidesModule'
    },

但是我有这个错误:

错误:未找到 TypesAidesAjouterComponent 的组件工厂.您是否将其添加到 @NgModule.entryComponents 中?

我找到了一个解决方案,它是移动删除 LazyLoading,但我的应用程序很大,不可能.

I have found a solution, it is to move remove the LazyLoading but my application is large and is not a possibility.

你有什么建议吗?

推荐答案

这是目前 Angular 的常见抱怨.在某些情况下,由于依赖注入的工作方式,从延迟加载的模块中提取小组件是不可能的.组件必须通过其各自的模块引入应用程序,这意味着如果模块尚未加载,您将无法使用它声明的任何组件.

This is a common complaint with Angular at the moment. In certain instances, extracting a small component out of a lazy loaded module is not possible because of how dependency injection works. Components have to be brought into the application through their respective Module, which means if the module hasn't been loaded, you can't use any of the Components it declares.

我不得不接受这样一个事实,即在结构良好的应用程序中确实很少出现这种需求,遇到它应该表明我需要重新评估应用程序的结构申请.

I had to come to terms with the fact that there really are very few instances where this need arises in a well structured application, and that bumping up against it should be a sign that I needed to re-evaluate the structure of the application.

解决此问题的最佳方法是提取组件并为其创建一个单独的模块,或者将其添加到导入到您在其中呈现它的模块中的共享模块中.

The best way to get around this issue is to extract the component and either create a separate module for it, or add it to a shared module that is imported into the module you are rendering it within.

这篇关于延迟加载模块中的 EntryComponents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:15