liveBook · Manning

Imports property

As explained in Pro Angular 16

The imports property is Angular module metadata containing the modules whose exported features are available to an Angular module.

The `imports` property in an `@NgModule` decorator lists the Angular modules a module depends on. It makes their exported features available to the importing module. An initial root module can use `imports: [BrowserModule]` to obtain the features needed to run in a browser; other imports are added as the application grows. section 19.2.1 section 5.2.2

Listing 5.17. The contents of the store.module.ts file in the src/app/store folder
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { ModelModule } from "../model/model.module";
import { StoreComponent } from "./store.component";
 
@NgModule({
    imports: [ModelModule, BrowserModule, FormsModule],
    declarations: [StoreComponent],
    exports: [StoreComponent]
})
export class StoreModule { }

The store module imports its data-model module alongside `BrowserModule` and `FormsModule`. Its `declarations` separately identifies `StoreComponent`, while `exports` makes that component available to other modules. section 5.4.2

Use `imports` for dependencies, not for classes defined by the module. A utility feature module imports its model module because its directives and pipes depend on model services. Its own directives and pipes belong in `declarations`; those intended for use by importing modules also belong in `exports`. section 19.3.2

Listing 7.5. The contents of the admin.module.ts file in the src/app/admin folder
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";
import { RouterModule } from "@angular/router";
import { AuthComponent } from "./auth.component";
import { AdminComponent } from "./admin.component";
 
let routing = RouterModule.forChild([
    { path: "auth", component: AuthComponent },
    { path: "main", component: AdminComponent },
    { path: "**", redirectTo: "auth" }
]);
 
@NgModule({
    imports: [CommonModule, FormsModule, routing],
    declarations: [AuthComponent, AdminComponent]
})
export class AdminModule { }

The administration module imports `CommonModule`, `FormsModule`, and a routing configuration produced by `RouterModule.forChild`. A dynamically loaded module must include its own route and component configuration, although it may depend on modules in the main application. Avoid making the main application depend on the dynamically loaded module, or its code will be bundled with the main application. section 7.1.1

How Pro Angular 16 covers it

How deep it goes
Works through root, store, utility, and dynamically loaded module configurations; stops short of explaining Angular’s internal module-loading mechanism.
Examples
Shows `BrowserModule` in a root module, model and forms dependencies in a store module, and child routes in an administration module.
What it assumes
Introduces the purpose of module metadata through examples that use TypeScript imports and `@NgModule` decorators.
What you can do after
Operate/configure it to give an Angular module the framework features, custom features, and child routes it needs.
How it connects
Connects `@NgModule` imports to feature modules, declarations, exports, services, and `RouterModule.forChild` routing.

Also explained in

Compare how 2 books explain imports property →

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.