Angular 2导入Http失败

时间:2017-03-20 12:25:10

标签: json angular http typescript

亲爱的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

2 个答案:

答案 0 :(得分:3)

您的导入存在不一致。

import {Injectable} from "angular2/core";

是测试版导入,而

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

是rc +

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

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

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

答案 1 :(得分:1)

从&#34; angular2 / core&#34 ;; // Beta版

导入{Injectable}

insted使用此

从&#34; @ angular / core&#34 ;; //最终版

导入{Injectable}