gpt4 book ai didi

javascript - Angular2 HTTP : Uncaught SyntaxError: Unexpected token < polyfills

转载 作者:行者123 更新时间:2023-12-03 07:45:32 24 4
gpt4 key购买 nike

所以我正在练习 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com