javascriptangularangular2-modules

Angular 2 difference between core and feature modules


I don't understand the difference between core and feature modules in angular 2. As far as I understand there are three recommended types of modules: core, feature and shared.

If a module exports some declarations (components, directives and pipes) and many modules will import this module, then this module should be a shared module (in shared directory).

If a module expors some declarations (components, directives and pipes) and only the root module will import this module, then this module should be a core module (in core directory).

Are feature modules the same? Only root module imports them. In this example there is a CoreModule and a feature module called ContactModule. I don't see the difference between them. Both of them are imported in root module.


Solution

  • core

    The core module contains providers for global services and can be guarded against being loaded from lazy loaded modules (as shown in your link) because this can easily cause bugs where lazy loaded modules get their own instance for global services (which is against the intention).

    feature As the name says - one module for one feature

    Otherwise, a feature module is distinguished primarily by its intent.

    A feature module delivers a cohesive set of functionality focused on an application business domain, a user workflow, a facility (forms, http, routing), or a collection of related utilities.

    shared

    This is mostly for convenience where several modules are exported so they can be made available at once in components that want to use them all (common pipes, components, and directives you probably want to use together in many other modules).