![]() |
VOOZH | about |
Let us first discuss about these terms:
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. |
| 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
This UserService is used as provider in app.module.ts.
app.module.ts
app.component.html
Output:
👁 Image