angular - 类型上不存在属性
问题描述
我面临一个奇怪的问题。所以我的函数authenticateUser
像这样返回和数组
{
success: false,
msg: "invalid password"
}
但是当我试图检查success == false
我是否收到错误类型 Objects 上没有此类属性时
this.authService.authenticateUser(user).subscribe(data=>{
if(data.success){//here need to check response for success
console.log(data)
this.authService.storeUserData(data);
this.router.navigate(['/user']);
}else{
this.router.navigate(['/login']);
}
我尝试使用其他教程中的示例,但仍然没有解决方案
解决方案
您有两种方法可以做到这一点:
选项 1 使用类型any
:
this.authService.authenticateUser(user).subscribe((data: any)=>{
if(data.success){//here need to check response for success
console.log(data)
this.authService.storeUserData(data);
this.router.navigate(['/user']);
}else{
this.router.navigate(['/login']);
}
}
或者做强类型{success: boolean, msg: string}
或创建这种类型的接口:
this.authService.authenticateUser(user).subscribe((data: {success: boolean, msg: string}) =>{
if(data.success){//here need to check response for success
console.log(data)
this.authService.storeUserData(data);
this.router.navigate(['/user']);
}else{
this.router.navigate(['/login']);
}
}
推荐阅读
- windows - 无法从 Windows 10 的 Cygwin 运行 HBASE
- python - 从字典数组中删除元素
- r - R 的 FDA Fourier() 函数 - 它是如何工作的?
- html - 找不到公共目录中的图像
- android - 在没有 Wifi 或 SIM 的情况下使用 HERE 定位
- java - 避免在兄弟类中重复代码
- python - PyQt TableView 从最后一列的前 2 行插入计算
- tensorflow - 如何将 'Conv-BN-Scale' 合并到一个 'Conv' 层以用于张量流?
- parameters - 如何使用 EZACFSM1 或任何其他方式扩展作业参数并将其写入数据集?
- javascript - 如何在 Objection.js 中管理验证与原始值