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

Bas*_*ijk 17 systemjs angular

我为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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Thi*_*ier 35

您需要检查是否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>
Run Code Online (Sandbox Code Playgroud)

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

import { HTTP_PROVIDERS } from 'angular2/http';

bootstrap(MyMainComponent, [ HTTP_PROVIDERS ]);
Run Code Online (Sandbox Code Playgroud)

希望它对你有帮助,蒂埃里

  • 很棒的想法,但没有奏效.但是,我将为此问题创建一个新问题 (2认同)