Angular 订阅 returns 未定义对象
Angular subscribe returns undefined object
所以我有 material 对话框,在这个对话框中我有按钮
<button mat-icon-button
(click)="getChoosenEmployee(element)">
</button>
元素类型为员工:
export interface Employee {
index: number;
employeeId: string;
firstName: string;
lastName: string;
}
当我关闭对话框时,我想将这个员工发送到父组件,这就是我正在做的:
getChoosenEmployee(employee: Employee) {
this.dialogRef.close(
employee
);
}
在我通过此函数接收的父组件中
dialogRef.afterClosed().subscribe(result => {
console.log(this.choosenEmployee);
this.choosenEmployees.push(this.choosenEmployee);
});
推送给我错误:
core.js:4197 ERROR TypeError: Cannot read property 'push' of undefined
at SafeSubscriber._next (add-workday-form.component.ts:72)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at Subject.next (Subject.js:39)
at SafeSubscriber._next (dialog.js:381)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
您是否初始化了 this.choosenEmployees
阵列?该错误表明 this.choosenEmployees
未定义,而不是 this.choosenEmployee
对象。
所以我有 material 对话框,在这个对话框中我有按钮
<button mat-icon-button
(click)="getChoosenEmployee(element)">
</button>
元素类型为员工:
export interface Employee {
index: number;
employeeId: string;
firstName: string;
lastName: string;
}
当我关闭对话框时,我想将这个员工发送到父组件,这就是我正在做的:
getChoosenEmployee(employee: Employee) {
this.dialogRef.close(
employee
);
}
在我通过此函数接收的父组件中
dialogRef.afterClosed().subscribe(result => {
console.log(this.choosenEmployee);
this.choosenEmployees.push(this.choosenEmployee);
});
推送给我错误:
core.js:4197 ERROR TypeError: Cannot read property 'push' of undefined
at SafeSubscriber._next (add-workday-form.component.ts:72)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at Subject.next (Subject.js:39)
at SafeSubscriber._next (dialog.js:381)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
您是否初始化了 this.choosenEmployees
阵列?该错误表明 this.choosenEmployees
未定义,而不是 this.choosenEmployee
对象。