Let us first discuss about these terms:
- Declarations:
- Declarations are used to declare components, directives, pipes that belongs to the current module.
- Everything inside declarations knows each other.
- Declarations are used to make directives (including components and pipes) from the current module available to other directives in the current module.
- Selectors of directives, components or pipes are only matched against the HTML if they are declared or imported.
Example of declaration:
import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], )} - Providers:
- Providers are used to make services and values known to dependency injection.
- They are added to the root scope and they are injected to other services or directives that have them as dependency.
Example of Provider:
import { StateService } from './services/state.service'; @NgModule({ providers: [ StateService ], )} - Imports:
- Imports makes the exported declarations of other modules available in the current module.
- It is used to import supporting modules likes FormsModule, RouterModule, CommonModule etc.
Example of Import:
import { BrowserModule } from '@angular/platform-browser'; @NgModule({ imports: [BrowserModule], )}
Differences:
|
Declarations |
Providers |
Imports |
| Declarations are used to make directives. | Providers are used to make services. | Imports makes the exported declarations of other modules available in the current module.i |
| Used to declare components, directives, pipes that belongs to the current module. | Used to inject the services required by components, directives, pipes in our module. | Used to import supporting modules likes FormsModule, RouterModule, etc. |
| Ex. AppComponent. | Ex. StateService. | Ex. BrowserModule. |
|
Defined in Declarations array in @NgModule @NgModule({ declarations: [ ], )} |
Defined in Providers array in @NgModule. @NgModule({ providers: [ ], )} |
Defined in imports array in @NgModule. @NgModule({ imports: [], )} |
An example in which all three declarations, imports and providers are used:
The providers used in this project is custom service named UserService.
ng g s User
user.service.ts
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root'}) export class UserService { constructor() { } } |
This UserService is used as provider in app.module.ts.
app.module.ts
// imports for the application import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { FormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; // declarations for the application import { AppComponent } from './app.component'; // providers for the application import { UserService } from './user.service'; @NgModule({ declarations: [ AppComponent, ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, FormsModule ], providers: [UserService], bootstrap: [AppComponent] }) export class AppModule { } |
app.component.html
<h1>GeeksforGeeks</h1> <ul> <li>imports in this app: BrowserModule, AppRoutingModule, HttpClientModule, FormsModule</li> <li>declarations in this app: AppComponent</li> <li>providers in this app: UserService</li> </ul> |
Output:
Recommended Posts:
- Difference between declarations and entryComponents in AngularJS
- Difference between node.js require and ES6 import and export
- What is entryComponents in angular ngModule ?
- How to import LESS through npm ?
- How to import SASS through npm ?
- CSS | @import rule
- SASS | @import
- ES6 | Import and Export
- What is the best way to include CSS file? Why use @import?
- How to Import another TypeScript Files ?
- How to import config.php file in a PHP script ?
- How to import Google Fonts in HTML ?
- import command in Linux with Examples
- Why import star in Python is a bad idea
- Different ways to import csv file in Pandas
- How to import data from .CSV file into MySQL table using Node.js ?
- What is the difference between MEAN.js and MEAN.io?
- What is the difference between (NaN != NaN) & (NaN !== NaN)?
- Difference between ASP and ASP.NET
- Difference between JSP and ASP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

