2016-04-26 7 views
0

コンストラクタ(プライベートhttp:Http)をコードに追加するたびに、エラーが発生します。何か案が?予期しないトークンプライベート

events.js:154 
     throw er; // Unhandled 'error' event 
    ^
MultipleErrors: app/services/loginService:7:15: Unexpected token private 
app/services/loginService:7:23: Unexpected token http 
app/services/loginService:7:27: Unexpected token : 
app/services/loginService:7:29: Semi-colon expected 
app/services/loginService:7:33: Semi-colon expected 
app/services/loginService:7:33: Unexpected token) 
app/services/loginService:7:35: Semi-colon expected 
app/services/loginService:26:16: Semi-colon expected 
app/services/loginService:32:17: Semi-colon expected 
app/services/loginService:36:1: Unexpected token End of File 

loginService.ts

import {Injectable} from 'angular2/core'; 
import {Http, Headers, Response} from 'angular2/http'; 
import {Observable}  from 'rxjs/Observable'; 

@Injectable() 
export class LoginService { 
constructor (private http: Http) {} 
    //tet 
    /* 
    login(path:string, username:string, password:string) { 
    var creds = "username=" + username + "&password=" + password; 
    var headers = new Headers(); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
    this.http.post('http://localhost:3001/sessions/create', creds, { 
     headers: headers 
     }) 
     .map(res => res.json()) 
     .subscribe(
     data => this.saveToLocal(data), 
     err => this.logError(err), 
     () => console.log('Authentication Complete') 
    ); 

    return this.http.get(path); 
    }*/ 
    saveToLocal(jwt) { 
    if (jwt) { 
     localStorage.setItem('id_token', jwt) 
    } 
    } 

    logError(err) { 
    console.error('There was an error: ' + err); 
    } 
} 

答えて

0

おそらく、あなたはES6代わりの活字体を使用しています。 ES6はこれをサポートしていません(プライベート/パブリックおよびタイプの可視性)、このようなエラーが発生します。 ES6で

、あなただけこれを使用することができます:依存性注入のためにまた

@Injectable() 
export class LoginService { 
    constructor (http) {} 

    (...) 
} 

を、ES6で、あなたはparametersプロパティの静的ゲッターを追加する必要があります。助けを

@Injectable() 
export class LoginService { 
    constructor (http) {} 

    (...) 

    static getter parameters() { 
    return [[Http]]; 
    } 
} 
+0

感謝をティエリーは、しかし、コードは私のケース、ES6のために働いていません。私はゲッターがES7のためだと思いますか?私はこのようにして、エクスポートクラスLoginService { static get parameters(){ return [[Http]]; } http:http; コンストラクタ(http){ this.http = http; console.log( 'http'、http); } – Hammer

+0

はい、あなたはES6で 'private'のサポートを持っていません;-) Ionic2を使用している場合は、TypeScriptを使用できるようにプロジェクトを生成するときに--tsオプションを使用できます... –

+0

残念ながら、 Ionic2を使用します。 tsを使用する場合、gulp + tsを持つ任意のシードからプロジェクトを再作成する必要がありますか? – Hammer

関連する問題