Modules in Angular
ANGULAR MODULES

Modules in Angular

Modules are a great way to organize an application and extend it with capabilities from external libraries.

NgModules consolidate components, directives, and pipes into cohesive blocks of functionality, each focused on a feature area, application business domain, workflow, or common collection of utilities.

Angular libraries are NgModules, such as BrowserModule, FormsModule, HttpClientModule, and RouterModule.

Modules can also add services to the application. Such services might be internally developed, like something you'd develop yourself or come from outside sources, such as the Angular router and HTTP client.

NgModule metadata

An NgModule is defined by a class decorated with @NgModule() decorator. The @NgModule() decorator is a function that takes a single metadata object, whose properties describe the module.

  • declarations - declares the components, directives and pipes used in the application.
  • exports - the declarations that could be used in the component templates of othermodules.
  • imports - the declarations of other modules that could be used in this module.
  • providers - the declarations of the singleton services that could be used throughout the application.
  • bootstrap - the root module sets the root component that hosts all other app view at the start of the application.
import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

 

import { AppComponent } from './app.component';

import { HeaderComponent } from './header/header.component';

 

@NgModule({

  declarations: [

    AppComponent,

    HeaderComponent

  ],

  imports: [

    BrowserModule

  ],

  providers: [],

  bootstrap: [AppComponent]

})

export class AppModule { }

A module can be any one of the following type:

  1. Root Module
  2. Feature Module
  3. Shared Module

Root Module vs Feature Module

Every Angular app has at least one NgModule class, the root module, which is conventionally named AppModule and resides in a file named app.module.ts. Root Module gets bootstrapped at the start of an Angular Application.

Feature modules are NgModules for the purpose of organizing code.A feature module is focused mainly on a specific application need such as a user workflow, routing, or forms.

While you can do everything within the root module, feature modules help you partition the app 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.

There are five general categories of feature modules which tend to fall into the following groups:

  • Domain feature modules.
  • Routed feature modules.
  • Routing modules.
  • Service feature modules.
  • Widget feature modules.

Shared Module

Shared Module comes into play when your application need same features to be implemented at different places. Shared module is mainly used to organize and streamline your code.You can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your app.

NgModules and JavaScript modules

Angular uses both the module system i.e; angular module system as well as the javascript module system all together.

The NgModule system is different from and unrelated to the JavaScript (ES2015) module system for managing collections of JavaScript objects.

In JavaScript each file is a module and all objects defined in the file belong to that module. The module declares some objects to be public by marking them with the export key word. Other JavaScript modules use import statements to access public objects from other modules.

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

However NgModules are classes decorated with @NgModule. The @NgModule decorator’s imports array tells Angular what other NgModules the current module needs. 

@NgModule({

  declarations: [

    AppComponent,

    HeaderComponent

  ],

  imports: [

    BrowserModule

  ],

  providers: [],

  bootstrap: [AppComponent]

})

In this way you're using the Angular and JavaScript module systems together. Although it's easy to confuse the two systems, which share the common vocabulary of "imports" and "exports", you will become familiar with the different contexts in which they are used.

Angular libraries

Angular loads as a collection of JavaScript modules. You can think of them as library modules. Each Angular library name begins with the @angular prefix. Install them with the npm package manager and import parts of them with JavaScript import statements.

Resource: https://angular.io

Modules in Angular lets you to organize your code, avoid duplication of same code and keep it maintainable. Share your experiences of how useful you found modules to be.

要查看或添加评论,请登录

Nagma Bano的更多文章

  • Angular Lifecycle Hooks

    Angular Lifecycle Hooks

    Angular Lifecycle Hooks are the different phases of the component from its creation to its destruction where we can…

  • Data Communication In Angular

    Data Communication In Angular

    Data sharing is one of the important concept to understand in Angular. How Angular communicates data from and to…

    4 条评论
  • Introduction to Data binding

    Introduction to Data binding

    Data-binding is the mechanism for coordinating the parts of a template with the parts of the component. You can achieve…

  • Templating, Styling and Component Hierarchy in Angular

    Templating, Styling and Component Hierarchy in Angular

    In Angular, your views are defined within HTML templates. A template is a form of HTML that tells Angular how to render…

    1 条评论
  • Introduction to Components

    Introduction to Components

    The core concept of any Angular application is the component. In effect, the whole application can be modeled as a tree…

    1 条评论
  • Bootstrapping of the Angular Application

    Bootstrapping of the Angular Application

    Seeing the complexity of the Angular file structure, the first question that comes to the mind is how does it actually…

    1 条评论
  • Angular Architecture

    Angular Architecture

    The main building blocks of Angular are: Modules Components Templates, Directives, and Data Binding Services and…

    1 条评论
  • Getting Started With Angular

    Getting Started With Angular

    To start working with Angular we need to first set up the development environment. A development environment is one…

    1 条评论
  • Angular Features and Benefits

    Angular Features and Benefits

    Angular is one of the most popular modern day web frameworks available today. Because of the sheer support of Google…

  • Angular6 Vs Angular5

    Angular6 Vs Angular5

    Angular 5 Angular 5 showed up on November 1, 2017 with the promise of speed and a smaller size. The key features of…

社区洞察

其他会员也浏览了