1

I am trying to implement routing in Angular 6 but there are issues with multiple levels of NgModules.

The structure of modules is the following (B and C are children of A):

A_Module
  B_Module
  C_Module

This is a segment of code from my top-level NgModule:

// A_Module
const routes = [
  { path: 'B', component: B_Component },
  { path: 'C', component: C_Component },
]

@NgModule({
  declarations: [
    A_Component
  ],
  imports: [
    B_Module,
    C_Module,
    RouterModule.forRoot(routes),
  ],
  providers: [],
  bootstrap: [A_Component]
})
export class A_Module { }

B_Component and C_Component are top-level components inside its own modules.

This solution doesn't work as there are no components from B_Module or C_Module declared inside this A_Module. The thing is that inside B_Module and C_Module there are many many components and moving everything to the A_Module is not the solution (that's why modules were made right - to clean and separate the code?).

Any solutions to that?

1 Answer 1

3

Export B_Component and C_Component in their Modules.

@NgModule({
  declarations: [BComponent],
  exports: [BComponent]
})
export class BModule {}

@NgModule({
  declarations: [CComponent],
  exports: [CComponent]
})
export class CModule {}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it works! I needed to remove BComponent and CComponent from AModule declarations as well but otherwise - works.
Glad I could help :) Please mark this as an answer if possible.
Will do but as you were so fast I have to wait for another 7 minutes :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.