所以我正在练习 http post 并从 YouTube 教程中获取 Angular2 的方法,但我无法解决问题 这是我的index.html 文件:
<html>
<head>
<title>MyTodo App</title>
<script src="node_modules/es6-shim/es6-shim.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
</head>
<body>
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app.js')
.then(null, console.error.bind(console));
</script>
<http-comp>Loading..</http-comp>
</body>
</html>
这是http服务文件:
import { Injectable } from 'angular2/core';
import { Http } from 'angular2/http';
import 'rxjs/add/operator/map' ;
@Injectable()
export class webhttpservice{
constructor(private http : Http){}
getfunctionfromapp(){
return this.http.get('www.google.com')
.map(res => res.json());
}
}
这是 app.ts 文件:
import { Component } from 'angular2/core';
import { bootstrap } from 'angular2/platform/browser';
import { webhttpservice } from './httpService';
import { HTTP_PROVIDERS } from 'angular2/http';
@Component({
selector : 'http-comp',
template : `
<button (click) = 'reqfunction()'>Get Request</button>
<p></p>
<br>
<button>Post Request</button>
<p></p>
`,
providers : [webhttpservice]
})
export class httpcomponent{
getdata;
constructor(private var1: webhttpservice){}
reqfunction(){
this.var1.getfunctionfromapp()
.subscribe(
data => this.getdata = JSON.stringify(data),
error => alert(error),
() => console.log('Finished')
);
}
}
bootstrap(httpcomponent, [HTTP_PROVIDERS]);
这些是我收到的原始错误
>Uncaught (in promise) TypeError: object is not a constructor(…)
httpService:1 Uncaught SyntaxError: Unexpected token <__exec @
angular2-polyfills.js:138
Uncaught SyntaxError: Unexpected token <
Evaluating http://localhost:3000/httpService
Error loading http://localhost:3000/app.jsrun
@ angular2-polyfills.js:138zoneBoundFn @ angular2-
最佳答案
加载包含 webhttpservice
类的 TypeScript 文件的请求有点奇怪:
Evaluating http://localhost:3000/httpService
应该是:http://localhost:3000/app/httpService.js根据您的配置...
您是否将httpService.ts
文件放在app
文件夹下?根据您的 SystemJS 配置,您应该这样做。
关于javascript - Angular2 HTTP : Uncaught SyntaxError: Unexpected token < polyfills,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35224751/