Angular 将事件传递/发出到父组件?
在Angular中,我可以创建发出动作的子组件:Angular 将事件传递/发出到父组件?,angular,angular-components,Angular,Angular Components,在Angular中,我可以创建发出动作的子组件: @Component({ ... template: ` <button (click)='someAction($event)'>Click Me</button> ` }) export class ChildComponent { @Output() onChildAction = new EventEmitter(); childAction() {
@Component({
...
template: `
<button (click)='someAction($event)'>Click Me</button>
`
})
export class ChildComponent {
@Output() onChildAction = new EventEmitter();
childAction() {
this.onChildAction.emit();
}
}
以及:
@组件({
...
模板:`
`
})
导出类ParentComponent{
parentAction(){
log('Hello');
}
}
事件立即发送到父级,而不必通过子处理程序。当您只需单击要让父组件处理的子组件中的事件时,这非常有用。完全可以从模板文件发出事件。事实上,当它是单行代码时,我也会这样做。这也很容易阅读
单击我
这里有一个是的,你可以这样做。你也可以考虑@bug我试过这个,但它似乎不起作用。让我试试again@TimmyOMahony这是一个快捷方式,我不知道为什么它以前对我不起作用,但是重新加载,一切都正常。谢谢
@Component({
...
template: `
<child-component (onChildAction)="parentAction()"></child-component>
`
})
export class ParentComponent {
parentAction() {
console.log('Hello');
}
}
@Component({
...
template: `
<button (click)='onChildAction.emit($event)'>Click Me</button>
`
})
export class ChildComponent {
@Output() onChildAction = new EventEmitter();
}
@Component({
...
template: `
<child-component (onChildAction)="parentAction()"></child-component>
`
})
export class ParentComponent {
parentAction() {
console.log('Hello');
}
}