我有一个带有两个组件子组件的父组件,我想知道哪个子组件生成了一个事件,我的尝试是绑定,但不起作用:
父:
<span> FIRST CHILD </span>
<childComponent [childName]=childName1></childComponent>
<span> SECOND CHILD </span>
<childComponent [childName]=childName2></childComponent>
export class ParentComponent implements OnInit {
childName1: string ="child1";
childName2: string ="child2";
}
在孩子:
<button (click)=event($event)> button </button>
export class ChildComponent implements OnInit {
@Input() childName: string;
}
...
event():void{
console.log(this.childName);
}
你也可以使用
<my-child (event)="onChildEvent($event, 'child1')"></my-child>
<my-child (event)="onChildEvent($event, 'child2')"></my-child>
这样孩子就不需要参与其中。