Home > Software design >  Angular dynamic component can't use component from different module
Angular dynamic component can't use component from different module

Time:03-31

This is the scenario (check this Stackblitz):

  • I have two modules: AppModule and OtherModule.
  • In AppComponent (from AppModule), I create two dynamic components, using FirstComponent and SecondComponent.
  • In the FirstComponent template I insert HelloComponent, that belongs to the same module (AppModule), and it works fine.
  • The problem comes in the SecondComponent, where I insert ThirdComponent, that belongs to OtherModule. I receive the error: 'third' is not a known element.
  • OtherModule is imported in AppModule.
  • If I remove <third></third> from second.component.html the app will run, but I need to insert ThirdComponent inside SecondComponent, and SecondComponent is dynamic.

So, how can I use inside a dynamic component a component from a different module?

Angular CLI: 12.0.1

CodePudding user response:

You have to export the components that throw errors. What I mean is that you need to include the third component in the exports array in OtherModule. It should work.

  • Related