Angular Module
In Angular, a module is a container for a group of related components, directives, pipes, and services. Modules help to organize and structure your Angular application by dividing it into smaller, more manageable pieces.
Each module in an Angular application is defined by an Angular module class, which is a class decorated with the @NgModule
decorator. The @NgModule
decorator takes an object with properties that define the module's behavior.
Here's an example of an Angular module class:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './my-component/my-component.component';
@NgModule({
declarations: [MyComponent], imports: [CommonModule], exports: [MyComponent]
})
export class MyModule { }
This module defines a single component called MyComponent
, and imports the CommonModule
from the Angular framework. The declarations
property lists the components, directives, and pipes that are part of the module, and the imports
property lists the modules that the current module depends on. The exports
property lists the components, directives, and pipes that should be available to other modules that import this module.
To use an Angular module in your application, you will need to import it into the root module of your application (usually the AppModule
). You can then use the components, directives, and pipes that are defined in the module in the templates of your application's components.
Create Module Using Angular CLI
To create an Angular module using the Angular CLI, you can use the ng generate module
command.
For example, to create an Angular module called MyModule
, you can run the following command:
ng generate module my-module
This will create a new module in the "src/app" directory of your project, with the following files:
my-module.module.ts
: The module class, written in TypeScript. This is where you can define the properties and behavior of the module.
You can also use the --routing
flag to generate a routing module for the module. A routing module is a module that defines routes for a set of components.
For example, to create a module with a routing module, you can run the following command:
ng generate module my-module --routing
This will create an additional file called my-module-routing.module.ts
that contains the routing module for the MyModule
module.