Angular2 Http错误

Mig*_*l S 14 angular

我开始通过他们在页面中提供的快速入门来学习Angular2 ,现在我正在尝试发出一些Http请求.但每当我引导组件时,Chrome都会给我这个错误:

Uncaught SyntaxError: Unexpected token <         http:1
Uncaught SyntaxError: Unexpected token <         angular2-polyfills.js:138
   Evaluating http://localhost:3000/angular2/http
   Error loading http://localhost:3000/app/boot.js
Run Code Online (Sandbox Code Playgroud)

这是组件:

import {Component} from 'angular2/core';
import {HTTP_PROVIDERS, Http} from 'angular2/http';

@Component({
  selector: 'users',
  providers: [HTTP_PROVIDERS],
  templateUrl: 'app/views/users.html'
})
export class UsersComponent {

  people: Object[];
  constructor(http: Http) {
    http.get('http://192.168.56.101/api/test').subscribe(res => {
      this.people = res.json();
      console.log(this.people);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

和引导:

import {bootstrap}    from 'angular2/platform/browser'
import {UsersComponent} from './components/users'

bootstrap(UsersComponent, [HTTP_PROVIDERS]);
Run Code Online (Sandbox Code Playgroud)

视图是唯一的{{people}}.

TypeScript正在编译好.我甚至不知道什么失败了!

小智 26

该部分缺乏文档.需要将Router和Http添加到index.html.容易犯错误


Egg*_*ggy 7

首先,如果您在引导程序上注入了提供程序,则不必在组件中再次执行此操作.

第二,你加入http.jsindex.html吗?

第三,你的代码中有错误.应该this.http.get()没有http.get()