Angular 5中的RequestOptions不推荐使用的符号错误 [英] RequestOptions deprecated symbol error in Angular 5
本文介绍了Angular 5中的RequestOptions不推荐使用的符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使Angular 4中的代码适应Angular 5.
我进行了许多更改,但是关于RequestOptions
时出现错误.
该代码与身份验证有关,这是我出现错误的地方:
I'm trying to adapt code in Angular 4 to Angular 5.
I made many changes but I had an error about RequestOptions
.
The code is about authentication and this is where I have the error:
import { Injectable } from '@angular/core';
import { RequestOptions} from '@angular/http';
import {User} from '../model/model.user';
import 'rxjs/add/operator/map';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable()
export class AuthService {
constructor(public http: HttpClient) { }
public logIn(user: User) {
const headers = new HttpHeaders();
headers.append('Accept', 'application/json')
// creating base64 encoded String from user name and password
const base64Credential: string = btoa( user.username + ':' + user.password);
headers.append('Authorization', 'Basic ' + base64Credential);
// this is where i'm having a problem :
const httpOptions = new RequestOptions();
httpOptions.headers = headers;
return this.http.get('http://localhost:8081/' + '/account/login' ,
httpOptions)
.map(resp => {
// login successful if there's a jwt token in the response
const user = resp.json().principal; // the returned user object is a principal object
if (user) {
// store user details in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
}
});
}
logOut() {
// remove user from local storage to log user out
return this.http.post('http://localhost:8081/' + 'logout', {} )
.map(resp => {
localStorage.removeItem('currentUser');
});
}
}
错误:使用了不赞成使用的符号
请帮助我更改Angular 5(RequestOptions
)
error : deprecated symbol used
please help me changing the code in Angular 5 (RequestOptions
)
推荐答案
您不应使用不推荐使用的模块@angular/http
中的RequestOptions
.
You shouldn't use RequestOptions
from the deprecated module @angular/http
.
如 API文档所示,options
现在属于以下类型:
As indicated in the API documentation the options
are now of the following type:
{
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: HttpObserve;
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
withCredentials?: boolean;
}
因此,您应该写:
const headers = new HttpHeaders();
headers.append('Accept', 'application/json')
const base64Credential: string = btoa( user.username + ':' + user.password);
headers.append('Authorization', 'Basic ' + base64Credential);
this.http.get('http://localhost:8081/' + '/account/login', {
headers: headers
});
或者:
this.http.get('http://localhost:8081/' + '/account/login', {
headers: {
'Accept': 'application/json',
'Authorization': 'Basic ' + btoa(user.username + ':' + user.password)
}
});
这篇关于Angular 5中的RequestOptions不推荐使用的符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文