Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 difference between core and feature modules

Tags:

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.

like image 301
Ildar Avatar asked Sep 12 '16 09:09

Ildar


People also ask

What is the difference between root module and feature module?

While you can do everything within the root module, feature modules help you partition the application into focused areas. A feature module collaborates with the root module and with other modules through the services it provides and the components, directives, and pipes that it shares.

What is the difference between a shared module and a core module in Angular application would commonly use?

CoreModule should have only services and be imported only once in the AppModule . SharedModule should have anything but services and be imported in all modules that need the shared stuff (which could also be the AppModule ).

What is core modules in Angular?

The core module usually contains components that are used once in an Angular application, such as a navigation bar, loader, footer, etc. This module should be loaded globally in AppModule .

What is difference between module and component in Angular 2?

The Component is a fundamental block of Angular and multiple components will make up your application. A module can be a library for another module. For instance, the angular2/core library which is a primary Angular library module will be imported by another component.


1 Answers

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).

like image 105
Günter Zöchbauer Avatar answered Oct 05 '22 10:10

Günter Zöchbauer