http响应对象中没有方法 [英] No methods in http response object

查看:48
本文介绍了http响应对象中没有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ http.get返回的对象没有方法.例子: 我有班级模型

Object returned by $http.get has no methods. Example: I have my class model

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提取实验室

in my service I call http fetching lab

getLab(labId: number) {
    return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId).toPromise();
}

当我在某个组件中获得此方法时,方法isActive是未定义的,因此调用

and when I get this in some component, method isActive is undefined, so call

lab.isActive();

抛出异常. 有什么干净的解决方案吗?

throws exception. Is there any clean solution for this?

推荐答案

服务器仅返回具有已定义对象属性的形成的数据.它实际上并没有创建该对象的实例.

The server just returns data formed with properties from the defined object. It doesn't actually create an instance of the object.

尝试这样的事情:

this.lab = Object.assign(new Lab(), this.retrievedLab)

this.retrievedLab是服务器返回的数据.

Where this.retrievedLab is the data returned from the server.

这应该创建对象,然后将所有检索到的属性复制到其中.

This should create the object and then copy any of the retrieved properties into it.

这篇关于http响应对象中没有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆