【发布时间】:2019-12-17 02:33:18
【问题描述】:
我正在研究 Angular 并尝试使用以下代码添加基本身份验证和 jwt:
private authProvider(
username: string,
password: string
): Observable<IServerAuthResponse> {
let accessToken: IServerAuthResponse;
this.http.post<any>(CCommonUrl.LOGIN, {
username,
password
}, { observe: 'response' }).subscribe((res) => {
// console.log(res.json);
console.log(res.headers);
console.log(res.headers.get('Authorization'));
accessToken.Authorization = res.headers.get('Authorization');
});
return of(accessToken);
界面
export interface IServerAuthResponse {
Authorization: string;
}
现在console.log 正在显示令牌,但我无法为accessToken.Authorization 赋值。代码失败并出现以下错误
core.js:7187 错误类型错误:无法设置属性“授权” 不明确的 在 SafeSubscriber._next (auth.service.ts:60)
任何见解都会有所帮助
编辑:我只需要在界面中获得价值。由于令牌进入标头,因此我无法将其直接映射到接口]
Edit2:就重复而言,该问题具有类,但在这种情况下它的接口
【问题讨论】:
-
因为accessToken没有初始化。
-
您是否在任何地方创建了
accessToken?你已经声明了它,但我认为它不会在任何地方实例化,除非我遗漏了什么。 -
AccessToken 未初始化导致问题。详见stackoverflow.com/a/23413375/9958058评论。另外,另一种方法是使用类而不是接口。
标签: angular typescript angular8