我在编译/构建
时在我的angular2组件上看到以下错误import { Component, OnInit } from '@angular/core';
import { Router, Params } from '@angular/router';
import {QueuedService} from '../queued.service';
@Component({
selector: 'app-reservation',
templateUrl: './reservation.component.html',
styleUrls: ['./reservation.component.css']
})
export class ReservationComponent implements OnInit {
username: string;
useremail: string;
route$: Router;
queued$: QueuedService;
constructor(route: Router, queued: QueuedService) {
this.queued$ = queued;
this.route$ = route;
this.queued$.getUserInfo()
.subscribe(
result => {
this.username = result.displayName;
this.useremail = result.emails[0].value;
}
);
}
ngOnInit() {
}
}
错误如下
Property 'displayName' does not exist on type '() => any'.
Property 'emails' does not exist on type '() => any'.
我无法确定问题所在?
修改 我的getUserInfo函数
getUserInfo() {
return this.http.get( this.host + '/auth/userdata')
.map(response => response.json());
}
答案 0 :(得分:2)
您的结果是any类型,只需将这两个变量更改为any的类型,
username: any;
useremail: any;
修改强>
在结果中添加任意类型
this.queued$.getUserInfo()
.subscribe(
(result: any) => {
this.username = result.displayName;
this.useremail = result.emails[0].value;
}
);
答案 1 :(得分:0)
getUserInfo()
没有声明具有这些属性的返回类型。我建议你在getUserInfo()
内进行子动作,这样你就不会与无类型的对象交叉任何函数边界。