【发布时间】:2019-06-11 09:12:23
【问题描述】:
我正在尝试填充模型,但收到错误无法分配给“GetUserByIdModel”,因为它不是变量。
这是我的模特
export class GetUserByIdModel {
firstname: string;
lastname: string;
nationalityId: number;
identityTypeId: number;
identityValue: string;
titleId: number;
genderId: number;
contactChannels: Array<ContactChannel>;
}
export class ContactChannel {
type: number;
value: string;
}
然后我将其导入服务文件并尝试使用它
声明
userDetails: GetUserByIdModel;
功能
getUserDetails(userId): Observable<GetUserByIdModel> {l
const token = this._storeService.getStoredData().id_token;
const headers = new HttpHeaders()
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json');
const options = { headers: headers };
const url = `${this.candidateUrl}/Get/${userId}`;
this._http.get<any>(url, options).subscribe((resp) => {
this.userDetails: GetUserByIdModel = {
firstname: resp.firstname,
lastname: resp.lastname,
nationalityId: resp.firstname,
identityTypeId: resp.identityTypeId,
identityValue: resp.identityValue,
titleId: resp.titleId,
genderId: resp.genderId,
contactChannels: [
{ type: 1, value: resp[0].value },
{ type: 2, value: resp[1].value }
],
};
})
return new Observable((observer) => {
observer.next(this.userDetails);
observer.complete();
});
}
我在这一行看到错误this.userDetails: GetUserByIdModel = {...
请帮忙
【问题讨论】:
-
尝试将
this._http.get<any>更改为this._http.get<GetUserByIdModel> -
试过了,还是一样的错误:(
-
使类中的所有字段都可以为空。喜欢名字?:字符串;姓氏?:字符串;在 ContactChannel 类中也相同或更好地使用接口而不是类
标签: angular typescript