Angular 2 给出 SystemJS 找不到 /angular2/http 模块

Angular 2 gives SystemJS cannot find /angular2/http module

我已经为 Angular 2 创建了一个服务:

import {Injectable} from "angular2/core";
import {Http} from "angular2/http";
import {TaskModel} from "./tasks.model"

@Injectable()
export class TasksDataService {

  tasks:Array<any>;

  constructor(http:Http) {

    this.tasks = [
      new TaskModel("Dishes"),
      new TaskModel("Cleaning the garage"),
      new TaskModel("Mow the grass")
    ];
  }

  getTasks():Array<any> {
    return this.tasks;
  }
}

当我 运行 我的程序浏览器显示:

system.src.js:1049 GET http://localhost:3000/angular2/http.js 404 (Not Found)

我查阅了教程,它们以相同的方式包含 angular2/http。有人看到出了什么问题吗?

您需要检查是否将 http.dev.js 文件包含在主 HTML 文件中:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

然后您需要添加 HTTP_PROVIDERS 作为 bootstrap 函数的第二个参数:

import { HTTP_PROVIDERS } from 'angular2/http';

bootstrap(MyMainComponent, [ HTTP_PROVIDERS ]);

希望对你有帮助, 蒂埃里