2

I have a component 'A' which is added in Module M1 and exported which is being lazily loaded , Now I have another module M2 which has component 'B' which is using A (selector)

So as M1 is lazy loaded I'm getting error as

Component A is not a part of any NgModule

sudhir
  • 1,289
  • 3
  • 23
  • 39
  • yes this is what you will get. if you need to share common functionality you should create service for that and add that service to main module all the lazy loaded module will be able to use it. – Vinay Pandya Dec 06 '16 at 07:12

2 Answers2

1

What you should do is create another module, which you can call SharedModule. Export the component A from the SharedModule. Then import the SharedModule into both M1 and M2

  • Yes , but all shared will be loaded i.e not lazily. Is there any alternative? – sudhir Dec 06 '16 at 07:20
  • I believe not. But I think what you are trying to achieve is different. You want to use the component A in M2, which I believe is not lazy loaded. So you need the component A, i.e., without lazy loading. That's the purpose of building a SharedModule. – Sarthak Sharma Dec 06 '16 at 07:23
  • Actually M2 is also lazilty loaded. – sudhir Dec 06 '16 at 11:34
  • @SarthakSharma Simple question: I have a Shared Module as you suggest and two lazy modules (m1 and M2). Am I required to import SharedModule in both M1 and M2 or is it possible to import it in the AppModule directly (which is a parent of M1 and M2)? Looks like importing in a parent is not working for lazy loaded modules... but just wanted a confirmation... or maybe I am doing something wrong... – Vincent Pazeller Nov 20 '17 at 13:41
0

add your Component 'A' in M2 module's declarations

@NgModule({
  imports:      [
    SharedModule
  ],
  declarations: [ componentB, componentA ],
  exports:    [ componentB ]
})
export class moduleM2 { }
anshuVersatile
  • 1,893
  • 1
  • 9
  • 16