获取"error TS2339: Property 'user' does not exist on type 'Object'. "错误



错误 TS2339:类型"对象"上不存在属性"user"。

export class LoginComponent implements OnInit {
user = new BehaviorSubject<Userlogin>(null);
constructor( private route: ActivatedRoute,
private router: Router,private http:HttpClient) { }
ngOnInit() {
}
loginClicked(form:NgForm)
{
const email = form.value.email;
const password = form.value.password;
const login = { email :email,password:password};
//alert(password);
this.http.post('http://localhost:3007/api/signin',login).subscribe(responseData => {
console.log(responseData);
if(responseData.user.name)
{
this.handleAuthentication(responseData.user.email,responseData.user._id,responseData.user.name);
this.router.navigate(['/main']);
}
})
form.reset();
}

只需使用any绕过 TypeScript 类型检查:

this.http.post('http://localhost:3007/api/signin',login).subscribe(responseData: any => {

更好的做法是使用为响应数据创建的自定义类型或接口。

相关内容

最新更新