Angular 2 gives SystemJS cannot find /angular2/http module

我怕爱的太早我们不能终老 提交于 2019-12-04 16:06:03

问题


I have created a service for 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;
  }
}

When I run my program the browser shows:

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

I have looked up tutorials and they include angular2/http the same way. Does anyone see what is going wrong?


回答1:


You need to check that you include the http.dev.js file in your main HTML file:

<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>

You need then to add the HTTP_PROVIDERS as second parameter of the bootstrap function:

import { HTTP_PROVIDERS } from 'angular2/http';

bootstrap(MyMainComponent, [ HTTP_PROVIDERS ]);

Hope it helps you, Thierry



来源:https://stackoverflow.com/questions/34748257/angular-2-gives-systemjs-cannot-find-angular2-http-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!