Angular - @Output 不工作

Angular - @Output not working

我正在尝试使用 @Output 指令在子组件上单击按钮时通知父组件。以下是我的代码:

父视图

<app-perito-select *ngIf="peritoSelect" (cancel)="cancelPeritoAction()"></app-perito-select>

父控制器

...
cancelPeritoAction(){
    console.log('cancel inside parent');
    this.selectedAction = undefined;
  }

子控制器

...
@Output() cancelAction: EventEmitter<any> = new EventEmitter<any>();  
...
cancel(){
    console.log('cancel inside child');
    this.cancelAction.emit();
  }

我遵循了 this 教程,看起来很简单,但我没有到达父函数。我错过了什么?谢谢。

尝试更换

(cancel)="cancelPeritoAction()"

(cancelAction)="cancelPeritoAction()"

因为您的@Output 事件的名称是cancelAction

如果要保留(取消)用法,可以通过将其添加为字符串参数来重命名外部输出 属性:

@Output('cancel') cancelAction: EventEmitter<any> = new EventEmitter<any>();