错误:XHR错误(404 Not Found)加载angular2/http

pd *_*had 12 angular2-http angular-cli angular

我正在为我的Angular2应用程序使用angular-cli.每当我尝试在我的组件/服务中加载angular2/http时,cli终端中没有显示错误,但是在我的浏览器控制台中它显示了这个 -

获取http:// localhost:4200/angular2/http 404(未找到)

未处理的Promise拒绝:错误:XHR错误(404 Not Found) 在XMLHttpRequest.desc.set.wrapFn [as_onreadystatechange]上加载http:// localhost:4200/angular2/http(http:// localhost:4200/vendor/zone. js/dist/zone.js:769:30)在Zone.runTask的ZoneDelegate.invokeTask(http:// localhost:4200/vendor/zone.js/dist/zone.js:356:38)(http:// localhost:4200/vendor/zone.js/dist/zone.js:256:48)在XMLHttpRequest.ZoneTask.invoke(http:// localhost:4200/vendor/zone.js/dist/zone.js:423:34))从http:// localhost:4200/app/js-tree.component.js加载http:// localhost:4200/angular2/http为"angular2/http"时 出错 ; 区域:; 任务:Promise.then; 值:错误:错误:XHR错误(404 Not Found) 在XMLHttpRequest.desc.set.wrapFn [as_onreadystatechange](http:// localhost:4200/vendor/zone)上加载 http:// localhost:4200/angular2/http . js/dist/zone.js:769:30)在Zone.runTask的ZoneDelegate.invokeTask(http:// localhost:4200/vendor/zone.js/dist/zone.js:356:38)(http:// localhost:4200/vendor/zone.js/dist/zone.js:256:48)在XMLHttpRequest.ZoneTask.invoke(http:// localhost:4200/vendor/zone.js/dist/zone.js:423:34))从http:// localhost:4200/app/js-tree.component.js加载http:// localhost:4200/angular2/http为"angular2/http"时 出错

我的angular-cli版本是0.0.39

节点:4.2.2

这是我的system-config.ts

const map: any = {
};

/** User packages configuration. */
const packages: any = {
};


const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',

  // Thirdparty barrels.
  'rxjs',

  // App specific barrels.
  'app',
  'app/shared',
  'app/tree',
  'app/tree/item',
  /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
  cliSystemConfigPackages[barrelName] = { main: 'index' };
});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
  map: {
    '@angular': 'vendor/@angular',
    'rxjs': 'vendor/rxjs',
    'main': 'main.js'
  },
  packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });
Run Code Online (Sandbox Code Playgroud)

还有我的package.json

{
  "name": "pankha",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng server",
    "postinstall": "typings install",
    "lint": "tslint \"src/**/*.ts\"",
    "format": "clang-format -i -style=file --glob=src/**/*.ts",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0-rc.1",
    "@angular/compiler": "2.0.0-rc.1",
    "@angular/core": "2.0.0-rc.1",
    "@angular/platform-browser": "2.0.0-rc.1",
    "@angular/platform-browser-dynamic": "2.0.0-rc.1",
    "@angular/router": "2.0.0-rc.1",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.26",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "angular-cli": "0.0.*",
    "clang-format": "^1.0.35",
    "codelyzer": "0.0.14",
    "ember-cli-inject-live-reload": "^1.4.0",
    "jasmine-core": "^2.4.1",
    "jasmine-spec-reporter": "^2.4.0",
    "karma": "^0.13.15",
    "karma-chrome-launcher": "^0.2.3",
    "karma-jasmine": "^0.3.8",
    "protractor": "^3.3.0",
    "ts-node": "^0.5.5",
    "tslint": "^3.6.0",
    "typescript": "^1.8.10",
    "typings": "^0.8.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

和我的index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Pankha</title>
  <base href="/">
  {{content-for 'head'}}
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Service worker support is disabled by default.
       Install the worker script and uncomment to enable.
       Only enable service workers in production.
  <script type="text/javascript">
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/worker.js').catch(function(err) {
        console.log('Error installing service worker: ', err);
      });
    }
  </script>
  -->
</head>
<body>
  <pankha-app>Loading...</pankha-app>

  <script src="vendor/es6-shim/es6-shim.js"></script>
  <script src="vendor/reflect-metadata/Reflect.js"></script>
  <script src="vendor/systemjs/dist/system.src.js"></script>
  <script src="vendor/zone.js/dist/zone.js"></script>
  <!-- <script src="node_modules/angular2/bundles/http.dev.js"></script> -->




  <script>
    System.import('system-config.js').then(function () {
      System.import('main');
    }).catch(console.error.bind(console));
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 8

我认为您忘记将http.dev.js文件包含在主HTML文件中:

<script src="node_modules/angular2/bundles/http.dev.js"></script>
Run Code Online (Sandbox Code Playgroud)

请注意,这仅适用于beta版本(不是rc版本),但由于您使用该angular2/http模块,我猜您使用的是beta版本.


pd *_*had 6

我找到了解决方案.我没有使用angular2的beta版本所以我不得不打开我的package.json并在依赖项下添加这一行

"@angular/http": "2.0.0-rc.1",
Run Code Online (Sandbox Code Playgroud)

然后我打开我的终端并键入

npm install
Run Code Online (Sandbox Code Playgroud)

它更新了我的system-config.ts

const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',

  // Thirdparty barrels.
  'rxjs',

  // App specific barrels.
  'app',
  'app/shared',
  'app/tree',
  'app/tree/item',
  /** @cli-barrel */
];
Run Code Online (Sandbox Code Playgroud)

它在哪里

const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',

  // Thirdparty barrels.
  'rxjs',

  // App specific barrels.
  'app',
  'app/shared',
  'app/tree',
  'app/tree/item',
  /** @cli-barrel */
];
Run Code Online (Sandbox Code Playgroud)

  • 你救了我几个小时;) (2认同)