typescript 从子组件到父组件的事件发射器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 从子组件到父组件的事件发射器相关的知识,希望对你有一定的参考价值。

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

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.scss']
})
export class ChildComponent {

  enteredVariable = '';
  @Output() childVariableEmitter = new EventEmitter<string>();
  
  onAddVariable() {
    this.childVariableEmitter.emit(this.enteredVariable);
  }
}
<input type="text" name="variable" [(ngModel)]='enteredVariable'>
<app-child (childVariableEmitter)="getVariable($event)"></app-child>
import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.scss']
})
export class ParentComponent {
  parentVariable = '';

  getVariable(variableFromChild) {
    this.parentVariable = variableFromChild;
  }
}

以上是关于typescript 从子组件到父组件的事件发射器的主要内容,如果未能解决你的问题,请参考以下文章