【问题标题】:Angular2 HTTP: Uncaught SyntaxError: Unexpected token < polyfillsAngular2 HTTP: Uncaught SyntaxError: Unexpected token < polyfills
【发布时间】:2016-02-05 12:52:19
【问题描述】:

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

【问题讨论】:

  • www.google.com 不能说你有没有这样的网址。
  • 我应该作为对 url 的请求,所以请求 www.google.com 或从服务等是否重要,对吗?
  • 我的意思是没有协议的 url。
  • 它会在我们点击按钮时触发,但是应该显示第一个按钮(界面),并且它没有发生,我得到的所有错误都与 polyfills 和浏览器响应加载...

标签: javascript angular angular-http polyfills angular2-routing


【解决方案1】:

加载包含 webhttpservice 类的 TypeScript 文件的请求有点奇怪:

评估http://localhost:3000/httpService

应该是:http://localhost:3000/app/httpService.js 与您的配置...

您是否将httpService.ts 文件放在app 文件夹下?根据您的 SystemJS 配置,您应该。

【讨论】:

  • 我没有遵循任何结构,它只是根目录下的所有文件,没有任何文件夹,不,我没有在任何地方指定localhost:3000/httpService:(所有错误都与polyfilles有关,
  • 其实这就是问题所在。因为在您的 SystemJS 配置中,您配置了 app 包。这告诉您的应用程序在进行导入(例如import { webhttpservice } from './httpService';)时加载转译文件(例如httpService.js 用于httpService.ts)。您不能在根级别定义此规则,否则 angular2 模块也将使用此策略加载...
  • 那么我现在应该怎么做,因为我是第一次练习http并且从一开始就遵循这个结构
  • 你唯一想念的就是将应用程序的所有 TypeScript 文件放在 app 子文件夹下;-)
  • 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊做一些http的东西的时候为什么要这样做呢?
猜你喜欢
  • 1970-01-01
  • 2018-01-05
  • 2012-05-17
  • 2019-02-10
  • 2014-07-08
  • 2011-03-09
  • 2014-01-06
  • 2012-04-10
相关资源
最近更新 更多