【发布时间】:2020-03-10 06:26:23
【问题描述】:
我有一个 eventemitter 问题,即当我在子类中添加数据并通过 EventEmitter 将数据传递给父类时,我面临一个问题,即当子类添加时父类无法获取第一个数据,第一个数据是在子项中添加第二个数据时,子项仅添加到父项
子组件
saveData()
{
this.dialog.closeAll();
this.getData();
const body={
name:this.model.name,
age:this.model.age
}
// this.arrayCalendar.push(body);
this.calendarService.previewdata.emit(body)
}
父组件
handleDateClick(arg) {
const data={};
this.dialog.open(AddPatientComponent,{
data:{data},
width:"200px",
height:"300px"
})
this.calendarService.previewdata.subscribe(message => this.result = message);
console.log("DATA FROM PARENT"+JSON.stringify(this.result))
if(this.result)
{
this.calendarEvents = this.calendarEvents.concat({
title: this.result.name,
start: arg.date,
allDay: arg.allDay
})
}
}
服务
constructor() { }
public previewdata = new EventEmitter();
有人知道这背后的原因吗?
【问题讨论】:
-
通常,事件发射器的使用方式不同。无论如何,您应该将结果记录在订阅内而不是订阅外。
-
为什么在服务中使用事件发射器? EventEmitters 用于绑定到 HTML 中的事件。如果您在代码中订阅它,则可以使用某种
Subject
标签: angular