【问题标题】:Angular 2 gives SystemJS cannot find /angular2/http moduleAngular 2 让 SystemJS 找不到 /angular2/http 模块
【发布时间】:2016-01-12 15:59:02
【问题描述】:

我已经为 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。有谁知道出了什么问题?

【问题讨论】:

    标签: angular systemjs


    【解决方案1】:

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

    <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 作为引导函数的第二个参数:

    import { HTTP_PROVIDERS } from 'angular2/http';
    
    bootstrap(MyMainComponent, [ HTTP_PROVIDERS ]);
    

    希望对你有帮助, 蒂埃里

    【讨论】:

    • 谢谢!但这引发了另一个错误:angular.js:12477 错误:没有 Http 提供者! (任务 -> TasksDataService -> Http)。我读到将它作为组件的提供者包含在内,但这只是一个类。有什么线索吗?
    • 您需要添加 HTTP_PROVIDERS 作为引导函数的第二个参数 ;-)
    • 我已经读过了。但这是一个 Angular 1 混合应用程序。所以我的引导程序看起来像:adapter.bootstrap(document.body, ["app"]);并且 adapter.bootstrap(document.body, ["app", HTTP_PROVIDERS]) 不起作用:)
    • 也许尝试将它放在一个数组中:[ 'app', [ HTTP_PROVIDERS ] ]
    • 好主意,但没有奏效。不过我会为这个问题创建一个新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多