Angular 5 StaticInjectorError:[Http]

假装没事ソ 提交于 2019-12-11 06:56:42

问题


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")) } }

回答1:


Try to import HttpClientModule in your app.module.ts

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



回答2:


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



回答3:


enter image description here

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



来源:https://stackoverflow.com/questions/47581094/angular-5-staticinjectorerrorhttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!