Clear concept of EventEmitter Class

前端 未结 2 1654
温柔的废话
温柔的废话 2021-01-24 10:42

code into child component mycomponent.ts

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

@Component({
  selector: \'app-mycomponent\         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 11:01

    The Childcomponent emits a click event. The click event of your childcomponent is emitting a String, as you defined here:

     @Output() clicked = new EventEmitter();
    
      clickchild() {
        this.clicked.emit('This is Child Component Code!');
      }
    

    The payload of the event (a string in this case), can then be accessed by the parameter $event. You subscribe in your parent component to the click event by:

    
    
    onClicked(value: string) {
        this.childdata = value;
    }
    

    So the child component emits a click event, but it has to be recognized by the parent component (utilizing subscription), otherwise the parent would not know about the emitted event.

    See here for the docs: https://angular.io/api/core/EventEmitter

    Sagat

提交回复
热议问题