角订阅返回未定义的对象

时间:2020-08-30 13:55:51

标签: angular typescript rxjs angular-material subscription

所以我有一个材料对话框,在这个对话框中我有一个按钮

<button mat-icon-button 
 (click)="getChoosenEmployee(element)">
</button>

元素是Employee类型:

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)

1 个答案:

答案 0 :(得分:2)

您是否初始化了gcloud compute ssh --zone=us-central1-a ${user}@${instance_name_arg} 数组?该错误表明未定义this.choosenEmployees对象,而不是this.choosenEmployees对象。

相关问题