【发布时间】:2018-11-16 01:09:13
【问题描述】:
当我在服务的构造函数中使用Http 时,我遇到了上述错误。
员工服务代码
import { Component } from '@angular/core'
import { Injectable } from '@angular/core'
import { HttpModule } from '@angular/http';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/map';
@Injectable()
export class employeService {
constructor(public http: Http){ }
getEmployeename(): Observable<string> {
return this.http.get('http://localhost:53656/api/values/Getdata').map((resp: Response) =>
resp.json()
);
}
}
我在员工组件中直接调用服务(而不是来自app.module.ts 文件)
员工组件代码
import { Component, OnInit } from '@angular/core';
import { employeService } from './Service/service';
@Component({
templateUrl: './app.employee.html',
providers: [employeService]
})
export class EmployeeComponent implements OnInit {
empName: string;
constructor(private empService: employeService) { }
ngOnInit() {
this.empService.getEmployeename().subscribe(empName =>
this.empName = empName
);
}
}
我在控制台中有此错误消息:
core.js:1598 ERROR Error: Uncaught (in promise): Error:
StaticInjectorError(AppModule)[employeService -> Http]:
StaticInjectorError(Platform: core)[employeService -> Http]:
NullInjectorError: No provider for Http!
Error: StaticInjectorError(AppModule)[employeService -> Http]:
StaticInjectorError(Platform: core)[employeService -> Http]:
NullInjectorError: No provider for Http!
at
【问题讨论】:
标签: angular