#Error [NullInjectorError] in standalone app

7 messages · Page 1 of 1 (latest)

next current
#

Hello !

I've just tried to create an angular application on v17 but when creating a service for the API system an error systematically appears on the _HttpClient import!

test.component.ts

import { Component } from '@angular/core';
import { ApiService } from '../api.service';
import { HttpClientModule } from '@angular/common/http';

@Component({
  selector: 'app-test',
  standalone: true,
  imports: [HttpClientModule],
  templateUrl: './test.component.html',
  styleUrl: './test.component.scss'
})
export class TestComponent {
  constructor(private apiService: ApiService) {}

  ngOnInit() {
    this.apiService.getData().subscribe(data => {
      console.log(data);
    });
  }
}

Api.service.ts :

import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(private http: HttpClient) { }

  getData() {
    return this.http.get('https://api.example.com/data');
  }
}

Error :

ERROR NullInjectorError: R3InjectorError(Standalone[_TestComponent])[_ApiService -> _ApiService -> _HttpClient -> _HttpClient]: 
  NullInjectorError: No provider for _HttpClient!
    at NullInjector.get (core.mjs:1654:27)
    at R3Injector.get (core.mjs:3093:33)
    at R3Injector.get (core.mjs:3093:33)
    at injectInjectorOnly (core.mjs:1100:40)
    at Module.ɵɵinject (core.mjs:1106:42)
    at Object.ApiService_Factory [as factory] (api.service.ts:7:24)
    at core.mjs:3219:47
    at runInInjectorProfilerContext (core.mjs:866:9)
    at R3Injector.hydrate (core.mjs:3218:21)
    at R3Injector.get (core.mjs:3082:33)

note that I don't have an app.module (new test project created just now).

bronze garden
#

Hey there this is funny I just had the same question some hours ago

#

Hello, is provideHttpClient() added to your app.config.ts file?

#

this was my response that helped

#

Hope this helps pls let me know :)

next current