【问题标题】:Angular 2 import Http failedAngular 2 导入 Http 失败
【发布时间】:2017-08-11 17:28:28
【问题描述】:

尊敬的 Stackoverflow 社区,

我很想做一个 http 请求来获取一些 JSON 数据。

import {Injectable} from "angular2/core";
import { Http }  from '@angular/http';
import {Trainer} from "./trainer"

@Injectable()
export class TrainerService
{
    private baseUrl : string = 'http://10.161.48.16:8080/campus/trainers';

    constructor(private http : Http)
    {}

   getAllTrainers()
   {
       let trainer$ = this.http
      .get(this.baseUrl);
      return trainer$;
   }
}

但是控制台记录了这个:

17.03.20 13:17:00 404 GET /@angular/http

经过大量的互联网搜索,http 模块的导入似乎有问题,(尽管我的自动完成可以找到它上面的所有方法)。我也可能是错的。

我完全是新手,我真的找不到解决方案。

package.json

 {
  "name": "campus-webapp",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/http": "~2.4.0",
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings":"^0.6.8"
  }
}

typings.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "baseUrl": "map",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.6/angular2-polyfills.js"></script>
    <script src="https://code.angularjs.org/tools/system.js"></script>
    <script src="https://code.angularjs.org/tools/typescript.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.6/Rx.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.6/angular2.dev.js"></script>
    <script>
      System.config({
        transpiler: 'typescript',
        typescriptOptions: { emitDecoratorMetadata: true },
        packages: {'app': {defaultExtension: 'ts'}},
        map: { 'app': './app' }
      });
      System.import('app/service_main')
            .then(null, console.error.bind(console));
    </script>
  </head>
<body>
   <my-app>Loading...</my-app>
</body>
</html>

是的,我的代码在 app/ 而不是 src/app

【问题讨论】:

  • 可能是个愚蠢的问题,但是,您是否在应用模块中导入了HttpModule? :)

标签: json angular http typescript


【解决方案1】:

您的导入不一致。

import {Injectable} from "angular2/core";

是测试版导入,而

import { Http }  from '@angular/http';

是rc+

如果您使用的是 angular2 的 beta 版本(因为您在 index.html 中导入 https://code.angularjs.org/2.0.0-beta.6/angular2.dev.js),请确保从正确的包 (angular2/http) 中导入 Http

但我建议您将您的应用升级到最新版本并按照官方网站上的最新指南进行操作。

更新日志:https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02

【讨论】:

    【解决方案2】:

    import {Injectable} from "angular2/core";//Beta 版

    insted 使用这个

    从“@angular/core”导入{Injectable};//最终版本

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多