简体   繁体   English

如何将事件从父组件传递到子组件?

[英]How to pass events from parent to child component?

How to pass object (which contains event) from parent to child component and how to emit @Output to that event from child to parent component. 如何将对象(包含事件)从父对象传递到子组件,以及如何将@Output传递给该事件从子组件到父组件。

Parent Compoent 家长能力

ts file: ts文件:

Obj = { 'event': 'onSelect($event)'}; 
onSelect(val){console.log('from child to parent')}

html file: html文件:

<child-component [Obj]="Obj"></child-component>

Child Component 子组件

How to emit/trigger action, which was sent from Obj . Obj发送的如何发出/触发动作。

EventEmitter is what you're looking for 您正在寻找EventEmitter

Child TS 儿童TS

@Output onSelect: EventEmitter<Object> = new EventEmitter();

select(){
  // myObject is object you want to send to parent
  onSelect.emit(myObject);
}

Child HTML 子HTML

<a (click)="select()">Select this</a>

Parent HTML 父HTML

<child-block [onSelect]="onChildSelect($event)">

Parent TS 家长TS

//obj will contain object you've sent from your child 
onChildSelect(obj: Object){
   //your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM