查看控制台日志时,我收到此错误' 没有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
文件中。我还缺少什么?
答案 0 :(得分:2)
您似乎在data.service.ts
的导入Http
声明from
路径中输了错误,
而不是
import { Http } from '@angular/Http';
应该是
import { Http } from '@angular/http';