我正在使用HttpClient从json端点获取一个对象.在我获取它并订阅了observable后,我发现构造函数不在模型上运行,并且对象上的公共方法都是未定义的.如何让构造函数运行并且方法可用?
原文链接:https://www.f2er.com/angularjs/143230.htmlexport 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()); });