5
Module not found: Error: Can't resolve '@angular/http' even i used import {HttpClientModule} from '@angular/common/http';
6
  • your angular version Commented Dec 28, 2018 at 12:29
  • Try breaking the server by pressing Ctrl + C(on Windows) or Cmd + C(on Mac) and run the server again(ng serve) Commented Dec 28, 2018 at 12:29
  • try to npm install all your modules. Then make sure its actually in your node_modules folder inside of the @angular dependencies Commented Dec 28, 2018 at 12:31
  • Can you try import {HttpClient} from '@angular/common/http'; Commented Dec 28, 2018 at 12:57
  • Also you should put more information about your angular version Commented Dec 28, 2018 at 13:00

3 Answers 3

5

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...
    });
}

https://angular.io/guide/http


Sign up to request clarification or add additional context in comments.

4 Comments

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
import { map } from 'rxjs/operators'; getConfig() { return this.http.get(this.configUrl) .pipe( map((res: any) => res.json()) ); } @Ganny
@Ganny did you import { Response } from @angular/http?
Yes I used this as well!
1

import { HttpModule } from '@angular/http';- Does not work

Try the below one -

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

...
imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule
  ],
...

This worked for me.

Comments

0

useimport { HttpClient, HttpHeaders } from '@angular/common/http';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.