我構建了一個具有根模塊的應用程序,然後在此模塊下有3個子模塊。在模塊1中,有一個組件可以在模塊3中重複使用,但是,如果我直接訪問模塊3中的組件URL,組件將永遠不會加載(我認爲發生這種情況是因爲模塊1未加載)。我已經嘗試過的部件從模塊1導出和根模塊引導它,但我得到一個錯誤,指出組件選擇沒有被發現Angular 2組件不在不同模塊中加載
---編輯---
根模塊
@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{}
模塊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{}
請提供一些代碼,以便我們可以查看。 – DeborahK
@DeborahK完成:) – julianomontini