2

I have been reading this tutorial on creating a feature module : https://johnpapa.net/introducing-angular-modules-feature-modules-2/.

In this tutorial FormsModule is imported by shared.module.ts and then exported. Why is that ?

Also why does shared.module.ts also exports CommonModule. Why is that?

Also does this mean that I have to export all my SharedModule's imports ?

Best regards

mp3por
  • 1,796
  • 3
  • 23
  • 35
  • Well, the shared module is a module to be shared among all modules if needed. Therefore, it can be used on other modules without you having to especify it on module declaration. – lenilsondc Nov 14 '16 at 18:40

1 Answers1

1

So that the importing module doesn't need to import them as well. I assume that using that module usually requires using CommonModule and FormsModule anyway and this way just adding ShardModule to imports: [] will do.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Why would I have to export them then ? When importing "SharedModule" shouldn't the DI thingy know that it depends on the FormsModule and make it available to the SharedModule ? What does any module importing the SharedModule care about the FormsModule which is an internal dependency of the SharedModule ? – mp3por Nov 14 '16 at 18:48
  • If it's only an internal dependency, then you shouldn't export it. Exporting it is only for convenience of the user when it is expected that `SharedModule` is always used toghether with `CommonModule` and `FormsModule` so that the user only needs 1 import instead of 3. – Günter Zöchbauer Nov 14 '16 at 18:50
  • But why would the user need to import CommonModule and FormsModule when importing SharedModule ? They are SharedModule's dependencies ? Shouldn't the DI automatically import them without me having to explicitly "export" them ? Does this mean also that I have to export all my SharedModule's "imports" – mp3por Nov 14 '16 at 18:53
  • There is no "need". I said "for convenience". – Günter Zöchbauer Nov 14 '16 at 18:54
  • So importing SharedModule will work even if I delete SharedModule's export statement ? – mp3por Nov 14 '16 at 18:56
  • Sure. But if you uses features contained in these exported modules directly, you'll have to add them to `imports: [...]` explicitly. – Günter Zöchbauer Nov 14 '16 at 18:57