我使用HttpClient從json端點獲取對象。在我獲取它並訂閱observable之後,我發現構造函數不能在模型上運行,並且對象上的公共方法都未定義。如何讓構造函數運行並且方法可用?HttpClient未運行構造函數
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());
});
爲什麼不只是''讓customerInstance = new Customer(customerResponse);''' – Sonicd300
是的,這是做到了。這與c#中的AutoMapper類似。 – Darthg8r