子组件html
<button nz-button (click)="childFunc(2)">按钮</button>
子组件component.ts
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.less']
})
export class ChildComponent implements OnInit {
@Output() childData=new EventEmitter();
constructor() { }
ngOnInit() {
}
childFunc(data:number){
this.childData.emit(data);
}
}
父组件html
<app-child (childData)="parentFunc($event)"></app-child>
子组件数据{{fromchildData}}
父组件component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less']
})
export class AppComponent {
fromchildData=null;
parentFunc(childData:number){
this.fromchildData=childData;
}
}
该文章展示了在Angular应用程序中,如何使用EventEmitter实现从子组件向父组件传递数据。子组件通过定义一个带@Output装饰器的方法并发射事件,父组件通过监听这个事件来接收并处理数据。

2859

被折叠的 条评论
为什么被折叠?



