Having recently updated my Angular project from version 9 to 15, I encountered the following issue. Any assistance would be greatly appreciated as I have been struggling with it for over 2 weeks.
The problem lies within app-lib.module.ts in the Angular library.
import { HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: HttpService,
multi: true
}, ],
Another problematic file is hero.service.ts in my Angular library.
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class HeroService {
texting: string = 'Wow!';
constructor(private http: HttpClient) { }
getResult() {
return 'Wow!!!!!!';
}
}
The error occurs when including (private http: HttpClient) in the constructor as shown above. Removing it allows the app to function properly.
Error: Uncaught (in promise): Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`. Find more at https://angular.io/errors/NG0203
Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`.
Following the update from Angular 9 to 15 in one of my projects, this issue arose and any guidance on resolving it would be highly appreciated. I've been grappling with it for quite some time now.