What is the difference between declarations, providers, and import in NgModule?
Answers (1)
Add AnswerDeclaration :
Declaration are used to make directives.
Used to declare components, directives, pipes.
Providers :
Provides are used to make services.
Used to inject services required in component.
Imports :
Import are used to import exported declaration of the other component module to the current module.
import supporting modules likes FormsModule, RouterModule.
Here is the example :
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’;
import { AppComponent } from ‘./app.component’;
import { UserService } from ‘./user.service’;
@NgModule({
// declarations for the application
declarations: [
AppComponent,
],
//Imports for the application
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
FormsModule
],
// providers for the application
providers: [UserService],
bootstrap: [AppComponent]
})
export class AppModule { }