尝试使用 angular2/Http 时出错

Error when using trying to use angular2/Http

我已经建立了一个项目,它是 quickstart 的副本。现在我想尝试加载一些 JSON 所以我将 app.component.ts 修改为:

import {Component} from 'angular2/core';
import {Http} from "angular2/http";

@Component({
    selector: 'my-app',
    template: '<h1>Success</h1>'
})
export class AppComponent{
    constructor(private _http:Http){

    }
}

添加 import 没问题,但是一旦我将 private http:Http 添加到我的构造函数中,我就会收到错误消息:

Uncaught SyntaxError: Unexpected token <         http:1

Uncaught SyntaxError: Unexpected token <         angular2-polyfills.js:1243
Evaluating http://localhost:3000/angular2/http
Error loading http://localhost:3000/app/main.js

我猜有些东西编译不正确,但我是 Angular 的新手,Typescript 的新手,Node 的新手——基本上我真的可以在这里得到一些帮助.

任何人都可以告诉我哪里出了问题,更重要的是 为什么 它是错误的以及我如何修复它并加载一些 JSON 来玩。

提前谢谢大家:)

我认为您忘记了将 Angular2 的 HTTP 模块的 http.dev.js 文件包含到主 HTML 文件中。

<script src="node_modules/angular2/bundles/http.dev.js"></script>

该错误是 SystemJS 尝试加载此模块时出现 404 错误的结果。

此外,您需要在引导应用程序时包含 HTTP 提供程序:

import {HTTP_PROVIDERS} from 'angular2/http';
(...)

bootstrap(AppComponent, [ HTTP_PROVIDERS ]);