![](/img/trans.png)
[英]How Angular can not find the component factory for my component despite it has been declared in the entryComponents option of my Module?
[英]How to use component from module in entryComponents another module?
我有ngx-bootstrap
模态组件。 此模式用于shared
文件夹中。 我在DashboardModule
使用这个FirstModalComponent
,如:
// Dashboard.module.ts
import { FirstModalComponent } from '../../shared/modals/first-modal/first-modal.component';
@NgModule({
declarations: [
DashboardComponent
],
imports: [
CommonModule,
ReactiveFormsModule,
RouterModule.forChild(routes),
SharedModule
],
entryComponents: [
FirstModalComponent
]
});
如果我想将我的FirstModalComponent
作为模块,我应该如何在我的DashboardModule
实现它并在entryComponents
定义它?
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FirstModalModule } from './first-modal/first-modal.module';
@NgModule({
declarations: [],
imports: [
CommonModule,
FirstModalModule
],
exports: [
CommonModule,
FirstModalModule
]
})
export class ModalsModule { }
然后我在SharedModule
导入/导出此ModalsModule
并尝试将共享模块导入DashboardModule
。
我现在如何将我的FirstModalComponent
注入FirstModalComponent
中的entryComponents
?
当我尝试导入FirstModalComponent
并将其放入entryComponents
时,我收到错误: Component is part of the declaration of 2 modules
。
PS我不确定将它作为模块是个好主意..
您应该只在一个module
声明FirstModalComponent
。 它可以是一个模块的一部分并由它们导出,并且可以定义为它自己的模块的entryComponent
。
对于您的情况,请将其声明为
entryComponents: [ FirstModalComponent ]
对于FirstModalModule
和导出FirstModalComponent
从FirstModalModule
。 当您在ModalsModule
或应用程序中的任何其他模块中导入FirstModalModule
时,它将按预期工作。
确保ModalsModule
和SharedModule
根据您的使用导出此FirstModalModule
。 如果要通过将SharedModule
导入DashboardModule
来使用它,则必须从SharedModule
导出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.