有
userGroups: IUserGroup[];
this.service.getUserGroups().subscribe(g => this.userGroups = g);
getUserGroups返回IUserDifferentGroup[]
,但IUserGroup
和IUserDifferentGroup
都有相同的字段IUserGroup
还有更多,如何将响应映射到新类型?
interface IUserDifferentGroup{
Name: string;
Code: string;
Id: number;
}
interface IUserGroup {
Id: number;
GroupName: string;
Visible: boolean;
IsDefault: boolean;
CanAssociated: boolean;
}
答案 0 :(得分:3)
在订阅内使用
this.service.getUserGroups().subscribe(g => this.userGroups = g as IUserGroup[]);
修改强>
根据评论,修改,
interface IUserDifferentGroup {
Name: string;
Code?: string; // <-- made this optional
Id: number;
}
interface IUserGroup {
Id: number;
GroupName: string;
Visible: boolean;
IsDefault: boolean;
CanAssociated: boolean;
}
然后更改订阅,
this.service.getUserGroups().map((g) => return {Name: g.GroupName, Id: g.Id})
.subscribe(g => {
this.userGroups = g as IUserGroup[];
});
看看它是否修复了它。