0
点赞
收藏
分享

微信扫一扫

Angular 子组件给父组件传递信息

子组件视图

<button (click)="sengMsg()">发送消息给父组件</button>

子组件:导入事件发射器:EventEmitter,导入Output

import { Component, OnInit, Input, Output,EventEmitter } from '@angular/core';

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit {
@Input() item: any;
@Output() childMsg=new EventEmitter();
constructor() { }
sengMsg(){
this.childMsg.emit({msg:"我是子组件,这是我发给父组件的消息"})
}

ngOnInit(): void {
}

}

父组件视图

<h1>子组件发来信息{{getChildMsg}}</h1>
<app-child [item]="sendchildMsg" (childMsg)="getEvent($event)"></app-child>

父组件

export class AppComponent {
msg="";
title = 'my-App';
sendchildMsg = '这是给子元素的数据,希望在子组件中显示';
getChildMsg = "";
getEvent(event:any){
console.log(event);
this.getChildMsg=event.msg;
}
}

Angular 子组件给父组件传递信息_html


举报

相关推荐

0 条评论