繁体   English   中英

Angular 6 Http订阅问题:类型“ Response”上不存在属性“ userInfo”

[英]Angular 6 Http Subscribe issue: Property 'userInfo' does not exist on type 'Response'

我正在使用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'; 导入文件。

对不起英语不好。 任何更新或见解表示赞赏。 谢谢

您可以使用response['userInfo']默认响应没有名为userInfo的属性,您可以将响应类型强制转换为给定类型或执行上述操作。

如果我从问题中正确猜出,则在编译时会引发错误。 您是否正确定义了响应类型? 它看起来像

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> { ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM