typescript 组件交互:父组件监听子组件事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 组件交互:父组件监听子组件事件相关的知识,希望对你有一定的参考价值。
import { Component } from '@angular/core';
@Component({
selector: 'app-vote-taker',
template: `
<h2>Should mankind colonize the Universe?</h2>
<h3>Agree: {{agreed}}, Disagree: {{disagreed}}</h3>
<app-voter *ngFor="let voter of voters"
[name]="voter"
(onVoted)="onVoted($event)">
</app-voter>
`
})
export class VoteTakerComponent {
agreed = 0;
disagreed = 0;
voters = ['Mr. IQ', 'Ms. Universe', 'Bombasto'];
onVoted(agreed: boolean) {
agreed ? this.agreed++ : this.disagreed++;
}
}
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-voter',
template: `
<h4>{{name}}</h4>
<button (click)="vote(true)" [disabled]="voted">Agree</button>
<button (click)="vote(false)" [disabled]="voted">Disagree</button>
`
})
export class VoterComponent {
@Input() name: string;
@Output() onVoted = new EventEmitter<boolean>();
voted = false;
vote(agreed: boolean) {
this.onVoted.emit(agreed);
this.voted = true;
}
}
以上是关于typescript 组件交互:父组件监听子组件事件的主要内容,如果未能解决你的问题,请参考以下文章
父组件可以监听到子组件的生命周期吗?
typescript 组件交互:父组件调用@ViewChild
vue---父组件监听子组件并获取子组件的值(子组件多个值)
vue 子组件监听父组件的值并操作
VUE父组件监听子组件事件,同时传入参数
typescript 组件交互:通过ngOnChanges监听输入值变化