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

标签 javascript angular angular-http polyfills angular2-routing

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

相关文章:

javascript - 背景幻灯片图像显示不正确

php - iScroll.js 和 iOS 浏览器检测 - 仅向 iPad/iPhone 显示 iScroll.js 文件

javascript - 使用 map 将数组中的对象重新格式化为新元素数组

post - angular.js $http(或$resource)POST和transformRequest作为服务的请求示例

angularjs - 方法发布时的 $http 服务缓存

javascript - JSON 元素未加载到 AngularJS 页面上

javascript - 同一字符串的两个不同部分的不同字体系列

javascript - Angular 2 错误 "Unhandled Promise rejection: Template parse errors:"

javascript - 在自定义下拉菜单 Angular 5 中使用箭头导航?

javascript - 将 $http 响应分配给 $scope 时,AngularJS View 不会更新