Angular Standalone: NullInjectorError: No provider for _HttpClient




Asked on January 25, 2024
In my Angular standalone application, I am injecting HttpClient. I am getting error in browser console as

NullInjectorError: No provider for _HttpClient!

Find my service class code.

@Injectable({
    providedIn: 'root'
})
export class ProductService {
constructor(private http: HttpClient) { }
------
}





Replied on January 25, 2024
You are missing using provideHttpClient().

In Angular standalone application, configure HttpClient using provideHttpClient() as below.

//app.config.ts

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    ------
  ]
};


//main.ts

bootstrapApplication(AppComponent, appConfig)
  .catch((err) => console.error(err));




Replied on January 25, 2024
Worked it. Thanks.

Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us