我开始学习Angular2,学习他们在页面中提供的quickstart,现在我正在尝试制作一些Http requests。但是每当我引导组件时,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这是构成部分:
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);
});
}
}而鞋带:
import {bootstrap} from 'angular2/platform/browser'
import {UsersComponent} from './components/users'
bootstrap(UsersComponent, [HTTP_PROVIDERS]);视图仅为{{people}}。
TypeScript正在编译OK。我都不知道是什么失败了!
发布于 2015-12-28 14:30:26
这方面缺乏文件。路由器和Http需要添加到index.html中。容易犯错误
发布于 2015-12-26 00:14:43
首先,如果在引导程序上注入了提供程序,则不必在组件中再次执行此操作。
第二,你在你的index.html中包括了index.html吗?
第三,代码中有一个错误。应该有this.http.get()而不是http.get()
发布于 2016-04-28 18:50:32
在引导过程中,您不必导入HTTP_PROVIDERS依赖项。所以试试:-
import {HTTP_PROVIDERS} from 'angular2/http';
import {bootstrap} from 'angular2/platform/browser'
import {UsersComponent} from './components/users'
bootstrap(UsersComponent, [HTTP_PROVIDERS]);在索引文件中,您需要添加:-
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.11/http.dev.js"></script>用于路由和http
https://stackoverflow.com/questions/34466866
复制相似问题