我正在构建一个具有根模块的应用程序,然后在此模块下是 3 个子模块。在模块 1 中,有一个可以在模块 3 中重用的组件,但是,如果我直接访问模块 3 中的组件 URL,则永远不会加载该组件(我认为这是因为模块 1 没有加载)。我已经尝试从模块 1 中导出组件并在根模块中引导它,但是我收到一个错误,提示找不到组件选择器
- -编辑 - -
根模块
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ManagerModule,
LogadoModule,
GeralModule,
RouterModule.forRoot(routes, {useHash: true})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
模块 1
@NgModule({
declarations: [
GeralComponent,
GeralHeaderComponent,
LoginComponent,
AtividadesListagemComponent // -> COMPONENT TO BE SHARED
],
imports: [
CommonModule,
ReactiveFormsModule,
FormsModule,
GeralRoutingModule
],
providers: [GeralService],
exports: [GeralComponent],
bootstrap: [GeralComponent]
})
export class GeralModule{}
Module 3 // -> 在这个模块中使用共享组件
@NgModule({
declarations: [
LogadoComponent,
AtividadesInscritoComponent,
LogadoHeaderComponent
],
imports: [
CommonModule,
ReactiveFormsModule,
FormsModule,
LogadoRoutingModule
],
providers: [],
exports: [LogadoComponent],
bootstrap: [LogadoComponent]
})
export class LogadoModule{}
项目结构为:
根模块 模块 1 模块 2 模块 3
----编辑2 -----
共享模块
@NgModule({
imports: [CommonModule],
exports : [
CommonModule,
AtividadesListagemComponent
],
declarations: [AtividadesListagemComponent]
})
export class SharedModule { }
根模块
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ManagerModule,
LogadoModule,
GeralModule,
RouterModule.forRoot(routes, {useHash: true})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
模块 1
@NgModule({
declarations: [
GeralComponent,
GeralHeaderComponent,
LoginComponent
],
imports: [
CommonModule,
ReactiveFormsModule,
FormsModule,
GeralRoutingModule,
SharedModule
],
providers: [GeralService],
exports: [GeralComponent],
bootstrap: [GeralComponent]
})
export class GeralModule{}
模块 3
@NgModule({
declarations: [
LogadoComponent,
AtividadesInscritoComponent,
LogadoHeaderComponent
],
imports: [
CommonModule,
ReactiveFormsModule,
FormsModule,
LogadoRoutingModule,
SharedModule
],
providers: [],
exports: [LogadoComponent],
bootstrap: [LogadoComponent]
})
export class LogadoModule{}