【发布时间】:2016-04-24 14:55:21
【问题描述】:
我正在尝试对我的其他本地主机服务器进行 REST Api 调用,我已提供此服务:
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {GlobalService} from '../app.service';
@Injectable()
export class AuthenticationService {
constructor(private _globals: GlobalService) {}
constructor(private _http: Http) {}
globals = this._globals.getGlobals()
getAuthenticationData() {
this._http.get(globals.apiURL)
}
}
并在我的组件中调用它:
import { Component } from 'angular2/core';
import {AuthenticationService} from '../services/authentication.service';
@Component({
selector: 'wd-header';
templateUrl: 'app/templates/header.html'
providers: [AuthenticationService]
})
export class HeaderComponent {
constructor(private _authenticationService: AuthenticationService) {}
authentication = this._authenticationService.getAuthenticationData()
}
尽管出于某种原因,Angular 声称 Http 是未定义的:
异常:在 HeaderComponent 实例化期间出错!。 angular2.dev.js:22911:9
原始异常:TypeError:this._http 未定义
我做错了什么?
编辑以包含 main.ts:
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {App} from './app.component';
import {GlobalService} from './app.service';
bootstrap(App, [
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
GlobalService
]);
【问题讨论】:
-
您的
index.html中是否引用了http.dev.js文件?
标签: typescript angular