简体   繁体   English

尽管已在我的模块的 entryComponents 选项中声明了我的组件,但 Angular 如何找不到它的组件工厂?

[英]How Angular can not find the component factory for my component despite it has been declared in the entryComponents option of my Module?

I want to show a component in a ngx-bootstrap's modal but how can I dynamically create it ?我想在ngx-bootstrap 的模态中显示一个组件,但如何动态创建它?

I have tried to declare the component in the entryComponents option of the RegMktRiskHomeModule but it doesn't work either.我曾尝试申报的组件entryComponents中的选项RegMktRiskHomeModule但它也不起作用。 The only way I found for now is to declare the ScenariosGraphComponent in the AppModule just like I've done it in RegMktRiskCommonModule .我现在找到的唯一方法是在AppModule声明ScenariosGraphComponent ,就像我在RegMktRiskCommonModule所做的RegMktRiskCommonModule

Could that be related to the fact that RegMktRiskHomeModule is imported in HomeModule and that this HomeModule is lazy-loaded by the application ?这可能与在RegMktRiskHomeModule中导入HomeModule并且此HomeModule由应用程序延迟加载的事实有关吗?

Here is my code (roughly):这是我的代码(大致):

reg-mkt-risk-common.module.ts : reg-mkt-risk-common.module.ts

@NgModule({
    declarations: [
        ScenariosGraphComponent
    ],
    entryComponents: [
        ScenariosGraphComponent
    ],
    exports: [
        ScenariosGraphComponent
    ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class RegMktRiskCommonModule { }

reg-mkt-risk-home.module.ts : reg-mkt-risk-home.module.ts

@NgModule({
    declarations: [
        RegulatoryMarketRiskHomeComponent,
        OverviewGridComponent // <-- component who actually show ScenariosGraphComponent in a modal
    ],
    exports: [
        RegulatoryMarketRiskHomeComponent
    ],
    imports: [
        // ...,
        RegMktRiskCommonModule
    ],
    providers: [
        // ...
    ]
})
export class RegMktRiskHomeModule { }

Here is the actual error message:这是实际的错误消息:

ERROR Error: No component factory found for ScenariosGraphComponent. Did you add it to @NgModule.entryComponents?
    at noComponentFactoryError (core.js:9659)
    at CodegenComponentFactoryResolver.push../node_modules/@angular/core/fesm5/core.js.CodegenComponentFactoryResolver.resolveComponentFactory (core.js:9697)
    at ComponentLoader.push../node_modules/ngx-bootstrap/component-loader/fesm5/ngx-bootstrap-component-loader.js.ComponentLoader._getContentRef (ngx-bootstrap-component-loader.js:411)
    at ComponentLoader.push../node_modules/ngx-bootstrap/component-loader/fesm5/ngx-bootstrap-component-loader.js.ComponentLoader.show (ngx-bootstrap-component-loader.js:152)
    at BsModalService.push../node_modules/ngx-bootstrap/modal/fesm5/ngx-bootstrap-modal.js.BsModalService._showModal (ngx-bootstrap-modal.js:927)
    at BsModalService.push../node_modules/ngx-bootstrap/modal/fesm5/ngx-bootstrap-modal.js.BsModalService.show (ngx-bootstrap-modal.js:853)
    at Object.action (overview-grid.options.ts:48)
    at MenuItemComponent.push../node_modules/ag-grid-enterprise/dist/lib/menu/menuItemComponent.js.MenuItemComponent.onOptionSelected (menuItemComponent.js:102)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:16147)

Maybe you forgot to import the MatDialogModule in the module of the component that opens the dialog.也许您忘记在打开对话框的组件的模块中导入 MatDialogModule。 The error that Angular generates in that case can be misleading.在这种情况下,Angular 产生的错误可能会产生误导。

You need to change your code like this您需要像这样更改代码

@NgModule({
    declarations: [
        RegulatoryMarketRiskHomeComponent,
        OverviewGridComponent,
        ScenariosGraphComponent
    ],
    entryComponents: [
        ScenariosGraphComponent
    ],
    exports: [
        RegulatoryMarketRiskHomeComponent
    ],
    imports: [
        // ...,
        RegMktRiskCommonModule
    ],
    providers: [
        // ...
    ]
})
export class RegMktRiskHomeModule { }

So you can remove ScenariosGraphComponent in your RegMktRiskCommonModule因此,您可以删除 RegMktRiskCommonModule 中的 ScenariosGraphComponent

如果你的模块是延迟加载的并且你需要在延迟加载的模块之间共享服务,那么请你一个共享模块并在那里添加公共组件和服务,然后在应用模块中导入共享模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在entryComponents中使用模块中的组件另一个模块? - How to use component from module in entryComponents another module? 我的子组件如何在Angular 2中的父组件上调用方法? - How can my child component call a method on the parent component in Angular 2? 尽管 URL 正确,Angular Router 仍不加载我的组件 - Angular Router does not load my component despite correct URL 如何在Angular 4 / Ionic中的组件中正确导入管道模块? - How can I properly import a Pipe Module in my Component in Angular 4/Ionic? angular2找不到我的组件参考 - angular2 cannot find my component reference Detox 找不到已指定 testId 的自定义组件 - Detox can't find custom component that has been given testId 如何使用 angular 在我的组件中使用 datePipe? - how can I use datePipe in my component using angular? 如何注册其DOM元素已由另一个JavaScript库创建的Angular2组件? - How can I register an Angular2 Component whose DOM element has been created by another javascript library? Angular 中的组件被破坏时如何获取先前的值 - How to get previous value when component has been destroyed in Angular 角; 错误:组件 NewPharmacyComponent 不是任何 NgModule 的一部分,或者该模块尚未导入到您的模块中 - Angular; Error: Component NewPharmacyComponent is not part of any NgModule or the module has not been imported into your module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM