Javascript-对象分解-奇怪
这是指一个平均项目。在Angular中,讨论的两个模型是-服务和客户机Javascript-对象分解-奇怪,javascript,angular,destructuring,Javascript,Angular,Destructuring,这是指一个平均项目。在Angular中,讨论的两个模型是-服务和客户机 export class Service { _id?: string; servName: string; servCat: string; freq: string; fees: number; dueDay: number; dueMonth: number; } // This is same as the model in Node. export clas
export class Service {
_id?: string;
servName: string;
servCat: string;
freq: string;
fees: number;
dueDay: number;
dueMonth: number;
}
// This is same as the model in Node.
export class Client {
compName: string;
....
....
services?: [{
servId: string, // type ObjectId in Node, gets populated when data is fetched in Angular
fees: number
}];
}
// This model is used to handled the fetched client data which has populated services data
export class ClientWithProfile {
compName: string;
....
....
services?: Service[];
}
在Angular中,在编辑服务数组之后,当我试图保存客户端时
console.log('client', this.client) // of Type ClientWithProfile, i.e. with full services array
//Destructuring client object
const { services, ...otherProps } = this.client
console.log('otherProps', otherProps) // shows Client data without Services Array
// savingClient is of Type Client
this.savingClient = { ...otherProps }
console.log('savingClient', this.savingClient)
////?????? shows client data with Services array, with just servId and fees?????
/// How is that possible???
我无法理解以下内容:
在分解this.client
对象(它有完整的服务数组)之后,我得到一个常量otherProps
,没有完整的服务数组
当我将该otherProps
分配给另一个对象savingClient
时,为什么只有ID和费用的服务数组包含在savingClient
中?对象savingClient
从未在代码中的此语句之前创建过
有人能解释一下吗?破坏类是错误的,要么调用它们的接口并使用一个简单的对象,要么使用一些函数从一个对象导出另一个对象。。。太多的代码不清楚。如果从类型
ClientWithProfile
创建的savingClient
没有services
属性,那么savingClient
如何成为类型Client
呢?可能是不相关的,但services?:[{servId:string,fees:number}]
看起来是错误的。你是说服务吗?:{servId:string,fees:number}[]
?