用于LazyLoading的模块中的EntryComponents [英] EntryComponents in Module for LazyLoading

查看:161
本文介绍了用于LazyLoading的模块中的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);
      }
    });

我正在组件懒加载中:

{
        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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆