Module not found: Error: Can't resolve '@angular/http' even i used import {HttpClientModule} from '@angular/common/http';
3 Answers
in the root AppModule
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
// import HttpClientModule after BrowserModule.
HttpClientModule,
],
declarations: [
AppComponent,
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
In your Service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
getConfig() {
return this.http.get(this.configUrl);
}
}
In your Components
showConfig() {
this.configService.getConfig()
.subscribe((data: any) => {
// response...
});
}
4 Comments
Ganny
I got working with these stufs but i got a new error while using.map((response: Response) => response.json()) error : TypeError: response.json is not a function
Ergi Kodra
import { map } from 'rxjs/operators';
getConfig() { return this.http.get(this.configUrl) .pipe( map((res: any) => res.json()) ); } @GannyMitch Wilkins
@Ganny did you
import { Response } from @angular/http?Ganny
Yes I used this as well!
ng serve)import {HttpClient} from '@angular/common/http';