liveBook · Manning

Module

As explained in Pro Angular 16

A module is a self-contained software unit that groups related functionality and can be incorporated into an application.

In an Angular application, “module” has two meanings. A JavaScript module manages dependencies between files; an Angular module configures part of the application. Keeping the two distinct makes imports and application setup easier to follow. section 9.4.3

Every TypeScript or JavaScript file is treated as a JavaScript module. Its classes, functions, and variables are accessible only within that file by default. Mark a feature with `export` to make it available elsewhere, then use `import` to declare a dependency on it. A relative import begins with `./` and omits the file extension. During compilation, the development tools follow these dependencies and include required code in a JavaScript bundle downloaded by the browser. section 4.5.1 section 4.5 section 2.3.3

Listing 4.29. Importing specific types in the main.ts file in the src folder
import { Name, WeatherLocation } from "./modules/NameAndWeather";

let name = new Name("Adam", "Freeman");
let loc = new WeatherLocation("raining", "London");

console.log(name.nameMessage);
console.log(loc.weatherMessage);

The two named imports come from one file module; the path names the module without its `.ts` extension. section 4.5.1

An Angular module is a class decorated with `@NgModule`. Its configuration describes building blocks and dependencies to Angular. Every application has a root module; in an application created with `ng new`, `AppModule` fills that role. Its `bootstrap` property identifies the component Angular loads at startup. Feature modules are optional: use them to group related features and make a complex application easier to manage. section 9.4.3 section 19.0

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
 
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

The root-module listing shows `@NgModule` imports and declarations alongside the `bootstrap` entry for `AppComponent`. section 9.4.3

Collecting component-library features

When an administration area needs Angular Material, a dedicated Angular module can collect the Material features the area selects. The administration module then imports that module. This separates feature selection from the rest of the administration configuration, though a component library still requires the application to fit its expected model and can be difficult to customize. section 7.4

Do not mistake a feature module for a provider boundary: providers defined by a feature module are available as though they were defined by the root module. section 19.0

How Pro Angular 16 covers it

How deep it goes
Works through file-level imports and exports and shows root-module configuration; defers details of decorator properties.
Examples
Uses named imports between TypeScript files, an `AppModule` listing, and an administration module that selects Angular Material features.
What it assumes
Introduces the JavaScript and TypeScript features needed for Angular development without requiring prior familiarity with their module syntax.
What you can do after
Build it from scratch by exporting and importing file features, then configuring a root or optional feature module.
How it connects
Connects modules with imports, exports, JavaScript bundles, components, and services.

Also explained in

Compare how 35 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.