無法用字串索引“res”。我已經嘗試了我發現的其他修復,但它在我的代碼中使用的背景關系它無法識別之前定義的物件。
完整錯誤:“元素隱式具有'any'型別,因為''status''型別的運算式不能用于索引'Object'型別。'Object'型別上不存在屬性'status'.ts(7053) "
這里的錯誤:
if(res['status'] === 'success')
全功能:
export class LoginComponent implements OnInit {
public user : User;
constructor(private loginService: LoginService, private router: Router) {
this.user = new User();
}
validateLogin() {
if(this.user.username && this.user.password) {
this.loginService.validateLogin(this.user).subscribe(res => {
console.log('result is ', res);
if(res['status'] === 'success') {
this.router.navigate(['/home']);
} else {
alert('Wrong username password');
}
}, error => {
console.log('error is ', error);
});
} else {
alert('enter user name and password');
}
}
ngOnInit(): void {
}
}
uj5u.com熱心網友回復:
您可以嘗試注意打字稿 res 只是一種型別,可以有status欄位,它可能會有所幫助
if((res as {[key: string]: any})['status'] === 'success') {
this.router.navigate(['/home']);
} else {
alert('Wrong username password');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439990.html
標籤:javascript 有角度的 打字稿 目的
上一篇:在Node.js中,如何訪問Index.js中匯出的模塊?
下一篇:POSTMANPUT請求未更新值
