2016-02-05 32 views
0

因此,進出口練HTTP POST和從YouTube獲得教程的Angular2方法和我不能找出問題 這裏是我的index.html文件:Angular2 HTTP:未捕獲的SyntaxError:意外的標記<polyfills

<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- 
+0

'www.google.com'不能說如果你有網址喜歡它。 – Jai

+0

我應該得到URL的請求,所以請求www.google.com或從服務等問題,這是否正確? – blackHawk

+0

我的意思是沒有協議的網址。 – Jai

回答

1

請求加載包含您webhttpservice類打字稿文件有點奇怪:

Evaluating http://localhost:3000/httpService

它應該是:http://localhost:3000/app/httpService.js您的配置。 ..

您是否將httpService.ts文件置於app文件夾下?根據你的SystemJS配置,你應該。

+0

我沒有跟隨任何在沒有任何文件夾的情況下構建它的只是所有文件,沒有我沒有指定http:// localhost:3000/httpService任何地方:(所有的錯誤都與polyfilles有關, – blackHawk

+0

這實際上是這個問題,因爲在你的SystemJS配置中, 'app'包,這會告訴你的應用程序在進行導入時(例如從'./httpService';'import {webhttpservice}加載轉發的文件(例如'httpService.ts'的httpService.js') 。你不能在根級定義這個規則,否則angular2模塊也將使用這個策略加載...... –

+0

那麼我現在應該做什麼,因爲它的第一次我正在練習http並且一直遵循這個結構從開始 – blackHawk

相關問題