[英]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.