1

hello i am getting such an error what is the reason for this?

StaticInjectorError[Http]: StaticInjectorError[Http]: NullInjectorError: No provider for Http! at NullInjector.get (core.js:923) at resolveToken (core.js:1211) at tryResolveToken (core.js:1153)enter code here at StaticInjector.get (core.js:1024) at resolveToken (core.js:1211) at tryResolveToken (core.js:1153) at StaticInjector.get (core.js:1024) at resolveNgModuleDep (core.js:10585) at NgModuleRef.get (core.js:11806) at resolveDep (core.js:12302)

import { Injectable } from '@angular/core' import {Todo} from './Todo'

import {Http, Response,Headers,RequestOptions} from '@angular/Http' import {Observable} from 'rxjs/Observable' import 'rxjs/add/operator/do' import 'rxjs/add/operator/catch' import 'rxjs/add/operator/map'

@Injectable() export class TodoService{

constructor(private http: Http){}

todoUrl = "https://jsonplaceholder.typicode.com/todos";

getTodos():Observable{ return this.http.get("https://jsonplaceholder.typicode.com/todos") .map((res:Response)=>res.json()) .do(data=>console.log("TODOS LIST")) } }
Aravind
  • 40,391
  • 16
  • 91
  • 110
  • 1
    Please format your code and your issue is probably https://stackoverflow.com/questions/33721276/angular-2-no-provider-for-http Google is your friend ;) – AT82 Nov 30 '17 at 19:21
  • import `HttpModule` and add to imports array of the respective module – Aravind Nov 30 '17 at 19:27
  • 1
    Please add HttpModule into the imports:[] section of NgModule. It should work – Gary Dec 19 '17 at 06:11

3 Answers3

5

Try to import HttpClientModule in your app.module.ts

import { HttpClientModule } from '@angular/common/http'; 
Vignesh
  • 2,378
  • 3
  • 25
  • 48
  • Can we mark this as the best answer as it explains the exact cause of the issue? more information available here - https://www.codeproject.com/Tips/1213737/Upgrade-to-Angular-and-HttpClient?msg=5461146 – planet_hunter Mar 23 '18 at 10:03
  • 1
    dont forget to mention that `private http: Http` should be `private http: HttpClient` – mast3rd3mon May 22 '18 at 13:37
0

Import the HttpClientModule in the app.module.ts as mentioned in the example. Thanks World! :)

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

        @NgModule({
            imports: [ BrowserModule, HttpClientModule ],
            providers: [],
            declarations: [ AppComponent ],
            bootstrap: [ AppComponent ]
        })
        export default class AppModule { }

        platformBrowserDynamic().bootstrapModule(AppModule); 
Rinold
  • 343
  • 1
  • 2
  • 12
-2

enter image description here

You didn't import HttpModule in (maybe you app.module.ts) in NgModule > imports

Lili
  • 1
  • 3