我正在使用angular 2,我只是使用observable创建服务。 在控制台中,我看到一些警告。 这是我的服务
import { Injectable } from '@angular/core';
import {Http , Headers , Response , RequestOptions } from '@angular/http';
import {Phase} from '../classes/phase';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {PhaseParser} from '../parsers/phase-parser' ;
import * as globals from '../../globals';
import {User} from '../classes/user';
@Injectable()
export class LoginService {
postData: string;
puttData: string;
private headers = new Headers({'Content-Type': 'application/json'});
constructor( private _http: Http) { }
/*
// login to the application
login (body: User) {
const json = JSON.stringify(body);
const params = json;
const headers = new Headers();
headers.append('Content-type', 'application/json');
return this._http.post(globals.serverIp + '/auth', params, {
headers : headers
})
.map((res: Response) => res.json()).subscribe(data => this.postData = JSON.stringify(data),
error => console.log(JSON.stringify(error)),
() => console.log(JSON.stringify(this.postData)));
}*/
login(username: string, password: string): Observable<boolean> {
return this._http.post( globals.serverIp + '/auth', JSON.stringify({username: username, password: password}), {headers: this.headers})
.map((response: Response) => {
// login successful if there's a jwt token in the response
const token = response.json() && response.json().token;
if (token) {
// store username and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));
// return true to indicate successful login
return true;
} else {
// return false to indicate failed login
return false;
}
}).catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
getToken(): String {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
const token = currentUser && currentUser.token;
return token ? token : '';
}
logout(): void {
// clear token remove user from local storage to log user out
localStorage.removeItem('currentUser');
}
}
这个警告可以在我的项目中提供一些问题吗? 提前谢谢你