我试图将两个变量从子组件发送到父组件。
我将子选择器放置到父组件视图,在该视图中,我使用了子标记并将@output名称myOutput绑定到父组件中的getData函数,并将事件处理程序传递给它。
<app-form-test [myInput]='myInputString' (myOutput)="getData($event)"></app-form-test>父ts文件=>
getData(value) {
alert(value);
}子ts文件
export class FormTestComponent implements OnInit {
@Input() myInput: [];
@Output() myOutput: EventEmitter<string> = new EventEmitter();
firstOutputString = 'hello I am coming from child component'
secondOutputString = " I am second string";
ngOnInit() {
}
SendData() {
this.myOutput.emit(this.firstOutputString);
}
}在这里,我想将另一个变量secondOutputString从子组件中传递给父组件,我尝试使用
SendData() {
this.myOutput.emit(this.firstOutputString , this.secondOutputString);
}但我错了
发布于 2018-09-20 07:41:16
在子.ts中:
@Output() myOutput: EventEmitter<{firstOutputString: string, secondOutputString: string}> = new EventEmitter();
SendData() {
this.myOutput.emit({
firstOutputString: this.firstOutputString,
secondOutputString: this.secondOutputString
});
}在父母.ts中:
getData(value) {
console.log(value.firstOutputString);
console.log(value.secondOutputString);
}https://stackoverflow.com/questions/52419853
复制相似问题