Skip to content

Instantly share code, notes, and snippets.

@royling
Last active March 21, 2017 09:45
Show Gist options
  • Save royling/b3d17b76c684185d5143f4c9718301c3 to your computer and use it in GitHub Desktop.
Save royling/b3d17b76c684185d5143f4c9718301c3 to your computer and use it in GitHub Desktop.
NgModule pitfalls

http://blog.angular-university.io/angular2-ngmodule/

Modules are very useful, but beware of the following pitfalls:

  • do not redeclare a component, directive, etc. in more than one module
  • modules do not create their own DI context, so injectables are available also outside the module
  • unless the module is lazy loaded, in that case a separate DI context is created by the router to avoid accidental injectable overrides and prevent hard to troubleshoot bugs
  • if you have a shared module that needs to be added to a lazy loaded module, make sure that it does not have providers, because that would create duplicate service instances (unless that is the intended behavior)
@royling
Copy link
Author

royling commented Mar 21, 2017

  • providers: injectables (annotated by @Injectable()) available from this module's DI context (injector)
  • declarations: components/directives/pipes included/defined in this module
  • imports: modules depended by this module
  • exports: components/directives/pipes exported by this module (these are visible to another module which imports this module)
  • bootstrap: components for bootstrapping this module
  • entryComponents: components that should be compiled
  • schemas: ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment