【发布时间】:2017-12-12 21:25:19
【问题描述】:
我正在尝试将 Http post 响应转换为 Type 对象
来自服务器的 JSON 响应
{
"authenticated": false,
"admin": false
}
要进行类型转换的 Angular 类
export class UserRole {
authenticated: boolean;
admin: boolean;
}
HTTP 后调用
login() {
this.user.password = btoa(this.user.password);
this.http.post(this.url, this.user).subscribe(res => {
console.log(res);
});
if (this.userRole.admin) {
console.log('Going in admin');
this.authService.setLoggedIn(this.user.userId,true);
} else {
console.log('Going in else admin');
this.authService.setLoggedIn(this.user.userId,false);
}
this.router.navigateByUrl('/nav');
}
我在将订阅结果转换为 UserRole 对象时遇到问题,我是否需要使用 JSON.parse 或任何其他方法。
【问题讨论】:
标签: angular typescript