每当我向代码添加构造函数(private 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 :(得分:0)
也许您使用ES6而不是TypeScript。 ES6不支持此功能(私有/公共和类型的可见性)并且会抛出此类错误。
使用ES6,您只能使用:
@Injectable()
export class LoginService {
constructor (http) {}
(...)
}
此外,对于依赖注入,使用ES6,您需要为parameters
属性添加静态getter:
@Injectable()
export class LoginService {
constructor (http) {}
(...)
static getter parameters() {
return [[Http]];
}
}