Angular2 Rxjs 404错误

时间:2016-03-26 19:21:15

标签: angular rxjs systemjs

尝试启动Angular2应用程序时出现以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found)
angular2-polyfills.js:332 Error: Error: XHR error (404 Not Found) loading http://localhost:55707/rxjs(…)

这是我的index.html:

<!DOCTYPE html>
<html>
<head>
    <base href="/">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Streak Maker</title>
    <link rel="icon" type="image/png" href="/images/favicon.png" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <!-- IE required polyfills, in this exact order -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
    <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.js"></script>
    <script src="node_modules/angular2/bundles/http.dev.js"></script>
    <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          },
        },
      });
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>
</head>
<body>
    <app>Loading...</app>
</body>
</html>

boot.ts:

///<reference path="../node_modules/angular2/typings/browser.d.ts"/> 

import {App} from './app.component';
import {bootstrap}  from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {APP_PROVIDERS} from './app.module';

bootstrap(App, [
    //ROUTER_PROVIDERS,
    //HTTP_PROVIDERS,
    APP_PROVIDERS]);

我已经尝试在system.config中放置rxjs的路径(它不起作用),但由于我使用rxjs捆绑包,我不应该这样做。< / p>

2 个答案:

答案 0 :(得分:15)

问题出在您的streak-maker/src/StreakMaker.Web/App/message-board/message-board.service.ts文件

您应该导入rxjs模块,因为它在Rxjs库中不存在:

import {Http} from 'angular2/http';
import {Injectable} from 'angular2/core';
import "rxjs"; // <-----

@Injectable()
export class MessageBoardService {
  constructor(private _http: Http) { }

  get() {
    return this._http.get('/api/messageboard').map(res => {
        return res.json();
    });
  }
}

您应该使用以下代码(rxjs/Rx):

import {Http} from 'angular2/http';
import {Injectable} from 'angular2/core';
import "rxjs/Rx"; // <-----

@Injectable()
export class MessageBoardService {
  (...)
}

你是对的:包含Rx.js文件足以提供Rxjs模块。 SystemJS.config中不需要其他(显式)配置。

答案 1 :(得分:0)

正如HydTechie所述(11月25日的评论和16:22时的评论),错误信息可能会产生误导。

我的第一个问题是,CodeMagazine问题May Jun 2017 "From Zero to Crud in Angular: Part 1"使用导入错误输入了&rxjs / add / operator / throw&#39;实际路径应该是&r; rxjs / add / observable / throw&#39;。

但即使经过纠正,并尝试在此处和其他地方进行修复,我也没有从Chrome开发工具控制台获得准确的错误。它指责zone.js查看不存在的throw.js库。

当我在IE中查看相同的代码时,我发现我的componentUrl文件名中有一个type-o,由于某种原因,它从我的http服务中冒出来并且在那里被输出到控制台。

所以HydTechie的咆哮blogspot虽然不全面,却正确地表明真正的错误并不容易找到。

我发现我不需要在我的systemjs.config中添加角度快速启动默认rxjs:{defaultExtension:&#39; js&#39;}包条目以外的任何内容,并修复导入&#39 ; rxjs /添加/操作者/投掷&#39 ;;要导入&r; rxjs / add / obserable / throw&#39;;

// the product service will call the webapi for product collections
import { Injectable } from "@angular/core";
import { Http, Response } from "@angular/http";

import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw'; // fixed typo
来自systemjs.config.js的

// packages tells the System loader how to load when no filename and/or no 
extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
      './*.js': {
        loader: 'systemjs-angular-loader.js'
      }
    }
  },
  rxjs: {
    defaultExtension: 'js' // this is fine as is
  }
}