$ http.get返回的对象没有方法。例: 我有我的班级模特
export class Lab {
constructor(
public id: number,
public name: string,
public description: string,
public isActive: boolean,
public classes: Classes[]
) { }
isActive(lab: Lab) {
return this.isActive;
}
}
在我的服务中我打电话给http取件实验室
getLab(labId: number) {
return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId).toPromise();
}
当我在某个组件中得到它时,方法isActive未定义,所以调用
lab.isActive();
抛出异常。 对此有什么干净的解决方案吗?
答案 0 :(得分:6)
服务器只返回带有已定义对象属性的形成数据。它实际上并不创建对象的实例。
尝试这样的事情:
this.lab = Object.assign(new Lab(), this.retrievedLab)
其中this.retrievedLab
是从服务器返回的数据。
这应创建对象,然后将任何检索到的属性复制到其中。
答案 1 :(得分:1)
在您的来电服务中,您可以像@Deborahk提到的那样
getLab(labId: number) {
return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId)
.map(res => Object.assign(new Lab(), res))
.toPromise();
}
答案 2 :(得分:0)
您还可以拥有一个类并像这样扩展它:
getLab(labId: number) {
return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId)
.pipe(Lab.serializeResponseMap())
.toPromise();
}
类定义:
export class Lab extends SerializableMap {
static instanceType = Lab;
constructor(
public id: number,
public name: string,
public description: string,
public isActive: boolean,
public classes: Classes[]
) { super(); }
isActive(lab: Lab) {
return this.isActive;
}
}
class SerializableMap {
static instanceType: any;
static serializeResponseMap(): any {
const createInstance = (r) => {
return Object.assign(new this.instanceType(), r);
};
return map((respValue: any) => {
if (Array.isArray(respValue)) {
return respValue.map(r => createInstance(r));
}
return createInstance(respValue);
});
}
}
答案 3 :(得分:0)
此版本的灵感来自@Matjaz Hirsman的回复(谢谢!),并添加了深度克隆功能。
另外:与序列化(或实际上 De 串行化”)更像是Decorator模式。
getLab(labId: number) {
return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId)
.pipe(Lab.decoratingMap())
.toPromise();
}
课程:
export class Lab extends Decorable {
static instanceType = Lab;
constructor(
public id: number,
public name: string,
public description: string,
public isActive: boolean,
public classes: Classes[]
) { super(); }
isActive(lab: Lab) {
return this.isActive;
}
}
class Decorable {
static instanceType: any;
/**
* Convert single entity into fully-fledged object
* @param source js object
* @return fully-fledged HalResource
*/
static decorateSingle(source: any) {
const target = new this.instanceType();
for (const key in target) {
if (source[key]) {
if (target[key] && typeof target[key] === 'object') {
target[key] = Object.assign(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
};
/**
* Convert entity or array of entities into fully-fledged objects
* @param response js object (or objects)
* @return fully-fledged object (or objects)
*/
static decorate(response: any) {
if (Array.isArray(response)) {
return response.map(element => this.decorateSingle(element))
} else {
return this.decorateSingle(response);
}
}
/**
* Rx Map operator decorating the JS objects into fully-fledged objects
*/
static decoratingMap() {
return map((response: any) => this.decorate(response));
}
}