作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 Angular 4 中的代码调整为 Angular 5。我做了很多更改,但出现了关于 RequestOptions
的错误。该代码是关于身份验证的,这就是我遇到错误的地方:
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
)
最佳答案
您不应使用已弃用模块 @angular/http
中的 RequestOptions
。
如 API documentation 中所示选项
现在具有以下类型:
{
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)
}
});
关于angular5 - Angular 5 中的 RequestOptions 已弃用符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50489115/
我是一名优秀的程序员,十分优秀!