AppComponent.html:1错误错误:没有Http的提供者! at injectionError(core.es5.js:1169)

时间:2017-08-11 17:14:41

标签: javascript angular angular-http

查看控制台日志时,我收到此错误' 没有Http的提供商!     在injectError ',在我的Angular 4应用程序中尝试使用Http模块时。我的app.module.ts文件是

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { UserComponent } from './components/user/user.component';

import { DataService } from './services/data.service';

@NgModule({
declarations: [
AppComponent,
UserComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [DataService],
bootstrap: [AppComponent]
})
export class AppModule { }

我的服务文件' data.service.ts'是

import { Injectable } from '@angular/core';
import { Http } from '@angular/Http';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {

constructor(public http: Http) {
console.log('Data service connected...');
}

getPosts() {
return this.http.get('https//jsonplaceholder.typicode.com/posts')
.map(res => res.json());
}

}

如图所示,我已将HttpModule包含在app.module.ts文件中。我还缺少什么?

1 个答案:

答案 0 :(得分:2)

您似乎在data.service.ts的导入Http声明from路径中输了错误,

而不是

import { Http } from '@angular/Http';

应该是

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