我正在使用angular6
Service.ts
post(url, body, options): Observable<Response>{
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.post(this.endpoint + url, body, httpOptions)
.pipe(map(this.parseData))
.pipe(catchError(this.handleError));
}
loginService.ts
login(data) {
return this.httpClient.post('api/authenticate', data , {
'Content-Type': 'application/x-www-form-urlencoded'
});
}
login.ts
onSubmit() {
if(this.loginForm.invalid) {
return;
}else{
let data = {
'email': this.loginForm.value.email,
'password': this.loginForm.value.password
}
this.loginService.login(data).subscribe(
response => {
console.log(response.userInfo.name);
this.myRoute.navigate(['dashboard']);
},
error =>{
console.log(error);
}
)
}
}
我遇到错误Property 'userInfo' does not exist on type 'Response'.
,但是在我的http响应中,我是{"status":true,"userInfo":{"id":1,"email":"admin@admin","password":"123456","type":"admin","name":"Admin"},"message":"su"}
我什至不愿意删除我的地图功能,但订阅功能仍然遇到相同的错误。 Angular 2我没有遇到这种问题。将更新发布到angular 6时,我遇到了这个问题,甚至更新了import { map, catchError, tap } from 'rxjs/operators';
导入文件。
对不起,英语不好。任何更新或见解表示赞赏。 谢谢
答案 0 :(得分:1)
您可以使用response['userInfo']
。默认响应没有名为userInfo的属性,您可以将响应类型转换为给定类型或执行上述操作。
答案 1 :(得分:0)
如果我从问题中正确猜出,则在编译时会引发错误。您是否正确定义了响应类型?看起来应该像
type Response = {
status: boolean,
userInfo: {id: number, email: string, password: number, type: string, name: string},
message: string};
或者,如果要对其他类型的请求使用post()函数,请在服务中定义一个更通用的post函数,如下所示:
post(url, body, options): Observable<any> { ...