在main.ts
import { HTTP_PROVIDERS } from '@angular/http';
import { AppComponent } from './app.component';
bootstrap(AppComponent,[HTTP_PROVIDERS]);
在employee.service.ts
中import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import {Employee} from '../classes/employee';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class EmployeeService{
constructor(private http: Http){
this.http = http;
}
}
即使在调用构造函数后,我也将http视为未定义。
请帮助...........
答案 0 :(得分:1)
这是工作http://plnkr.co/edit/ymvo2AmnkpyWvcnJLMKC?p=preview
首先,使用@Injectable()标记您的服务,以便Angular运行时知道它应该由DI进行管理。
@Injectable()
export class EmployeeService{
constructor(private http: Http){
this.http = http;
}
}
其次,请确保您不要忘记将 HTTP_PROVIDERS 引导至您的应用:
import {HTTP_PROVIDERS} from '@angular/http';
...
bootstrap(AppComponent, [HTTP_PROVIDERS, EmployeeService]);
请参阅https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#injectable