复制后无法检索对象属性
Can't retrieve object properties after copying
我试图将数据从 angular 中的一个组件传递到另一个组件,第二个组件显示为 Material 对话框。在传递对象之前,它的行为符合预期,但是当我传递它时,除了第一个 属性.
之外,我失去了对所有内容的访问权限
路过class:
displayDetail(i) {
let dialogRef = this.dialog.open(PartDetailComponent, {
width: '90%',
data: { part: this.partList.find(x => x.partNumber = i) , id: i }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
});
}
接收class
constructor(public dialogRef: MatDialogRef<PartDetailComponent>,
@Inject(MAT_DIALOG_DATA) public data: Part) {
if (data == undefined) {
console.log("Bad data. Closing modal.")
}
else {
this.part = data;
console.log(data); // object now only has id
console.log(this.part); // identical to data
console.log(this.part.id); // displays id
console.log(this.part.description); // undefined
console.log(data.description); // undefined
}
}
我是 angular 的新手,在某种程度上是打字稿的新手,但这似乎不应该发生。这到底是怎么回事?
编辑:如果第 class 部分相关:
export class Part {
id: number; // assuming there's an ID unique of partNumber or sku
title: string;
partNumber: number;
description: string;
leadTime: number;
weight: number;
sku: number;
pictureUrl: string;
inventory: number;
inStock: boolean;
isOrderable: boolean;
instructions: string;
}
记录这个,因为我觉得围绕 angular 事情的文档太少了,这是一个有趣的陷阱。
冲突发生在我传递的对象类型和我接收的对象类型之间。
我正在发送:
data: { part: this.partList.find(x => x.partNumber = i) , id: i }
这显然是一个包含零件和 ID 的对象。
我正在尝试接收零件:
constructor(public dialogRef: MatDialogRef<PartDetailComponent>,
@Inject(MAT_DIALOG_DATA) public data: Part) {
所以当我将接收对象更改为 any 时,我就能够访问 data.part 并正确使用它。
constructor(public dialogRef: MatDialogRef<PartDetailComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
这更像是一个转译陷阱,因为没有组件间类型检查(就像在大多数情况下在 C# 等编译语言中一样)。转译器不检查,也不关心相互连接的组件是否正在传递 'wrong' 类型的数据,而 javascript 只是对接收到的数据做它能做的事情。
我试图将数据从 angular 中的一个组件传递到另一个组件,第二个组件显示为 Material 对话框。在传递对象之前,它的行为符合预期,但是当我传递它时,除了第一个 属性.
之外,我失去了对所有内容的访问权限路过class:
displayDetail(i) {
let dialogRef = this.dialog.open(PartDetailComponent, {
width: '90%',
data: { part: this.partList.find(x => x.partNumber = i) , id: i }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
});
}
接收class
constructor(public dialogRef: MatDialogRef<PartDetailComponent>,
@Inject(MAT_DIALOG_DATA) public data: Part) {
if (data == undefined) {
console.log("Bad data. Closing modal.")
}
else {
this.part = data;
console.log(data); // object now only has id
console.log(this.part); // identical to data
console.log(this.part.id); // displays id
console.log(this.part.description); // undefined
console.log(data.description); // undefined
}
}
我是 angular 的新手,在某种程度上是打字稿的新手,但这似乎不应该发生。这到底是怎么回事?
编辑:如果第 class 部分相关:
export class Part {
id: number; // assuming there's an ID unique of partNumber or sku
title: string;
partNumber: number;
description: string;
leadTime: number;
weight: number;
sku: number;
pictureUrl: string;
inventory: number;
inStock: boolean;
isOrderable: boolean;
instructions: string;
}
记录这个,因为我觉得围绕 angular 事情的文档太少了,这是一个有趣的陷阱。
冲突发生在我传递的对象类型和我接收的对象类型之间。
我正在发送:
data: { part: this.partList.find(x => x.partNumber = i) , id: i }
这显然是一个包含零件和 ID 的对象。
我正在尝试接收零件:
constructor(public dialogRef: MatDialogRef<PartDetailComponent>,
@Inject(MAT_DIALOG_DATA) public data: Part) {
所以当我将接收对象更改为 any 时,我就能够访问 data.part 并正确使用它。
constructor(public dialogRef: MatDialogRef<PartDetailComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
这更像是一个转译陷阱,因为没有组件间类型检查(就像在大多数情况下在 C# 等编译语言中一样)。转译器不检查,也不关心相互连接的组件是否正在传递 'wrong' 类型的数据,而 javascript 只是对接收到的数据做它能做的事情。