我正在使用 HttpClient 从 json 端点获取对象。在我获取它并订阅 observable 之后,我发现构造函数没有在模型上运行,并且对象上的公共方法都是未定义的。如何让构造函数运行并且方法可用?
export class Customer {
constructor() {
this.Addresses = new Array<Address>();
}
public Addresses: Array<Address>;
public addAddress(address: Address) void{
this.Addresses.push(address);
}
}
var url: string = `${this.urlBase}api/customer/${id}`;
var customerObservable: Observable<Customer> = this.authHttp.get<Customer>(url);
customerObservable.subscribe(customer => {
// Addresses is undefined!
customer.Addresses.push(new Address());
// addAddress is undefined!
customer.addAddress(new Address());
});