【发布时间】:2018-02-11 05:36:39
【问题描述】:
考虑以下代码:
export class Model {
constructor(input: any) {
this.deserialize(input);
}
deserialize(input: any): Model {
Object.assign(this, input);
return this;
}
}
export class Body extends Model {
Success: boolean;
@Relationship Result: Result;
//...
}
export class Result extends Model {
Skip: number;
Top: number;
TotalCount: number;
//...
}
脚本接收一个 json 并启动 Body 类的新实例:
//...
let body = new Body({
Sucess: true,
Result: {
Skip: 0,
Top: 0,
TotalCount: 20
//...
}
//...
});
deserialize 方法用于将所有可枚举自身属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
要正确启动Result 属性,需要更改deserialize 方法:
deserialize(input: any): Model {
Object.assign(this, input);
this.Result = new Result(input.Result);
return this;
}
Model类泛型无法实现描述的解决方案的问题。
我不想在每个子类上声明deserialize 方法。
因为问题可以使用装饰器“@Relationship”解决?
【问题讨论】:
标签: angular typescript