[英]How to emit an event and send an array to parent from modal component
In my vue application I have a modal where the content is another component (child) and sends an array to its parent component and the parent displays it.在我的 vue 应用程序中,我有一个模态,其中内容是另一个组件(子组件)并将数组发送到其父组件,父组件显示它。 I had done this with props which works, but learned it is called an anti.patern and is not recommended.我已经用 props 完成了这个工作,但了解到它被称为 anti.pattern 并且不推荐。 So I wanted to try it with events now.所以我现在想用事件来尝试它。 But I am kinda failing.但我有点失败。 I did the function to emit an event which calls the Method which fills the array with data.我做了这个函数来发出一个事件,该事件调用用数据填充数组的方法。 I binded the event with @ in my parent but there is no reaction or to be more precise the value from the event is undefined.我在我的父母中用 @ 绑定了事件,但没有反应,或者更准确地说,事件的值是未定义的。
Could someone look at it and tell me what is wrong.有人可以看看它并告诉我出了什么问题。 I am new to vue and events so I am a bit lost:我是 vue 和 events 的新手,所以我有点迷茫:
Child Event Method:子事件方法:
generateGroup(){
var pageURL = window.location.href;
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
const newMeeting = {meetingUrl: "", meetingName: "", date: this.selectedDate, startTime: "", endTime: ""};
newMeeting.meetingUrl = "https://jitsi.zim.uni-due.de/" + this.randomUrlIdGenerator().toString()
newMeeting.meetingName = lastURLSegment
this.model.forEach((model, i) => {
const key = `participant${i + 1}`;
newMeeting[key] = model.voterUniqueName;
newMeeting.startTime = model.startTime;
newMeeting.endTime = model.endTime
})
this.newGroup = [],
this.finalMeetingArray.push(newMeeting)
this.newGroup.push(newMeeting)
return this.newGroup
},
eventSetter(){
this.$emit('updateArray', this.generateGroup())
this.newGroup = []
this.selectedDate = ""
this.model = []
}
eventSetter(){
this.$emit('updateArray', this.generateGroup())
this.newGroup = []
this.selectedDate = ""
this.model = []
}
Parent html: this is where I am trying to bind my event and call a function:父 html:这是我尝试绑定事件并调用函数的地方:
<AddGroupsModal @updateArray="generateGroup" ref="addGroupModal"/>
Method:方法:
generateGroup(value){
if(this.meetingArrayBackend.length === 0){
console.log(this.meetingArrayBackend)
let lastElement = this.value
this.meetingsArray.concat(this.value)
console.log("THIS.MEETING WIRD GEPUSHT INHALT : " + this.value)
} else
{
let lastElement = this.value[this.value.length - 1]
console.log(value)
this.meetingsArray.push(lastElement)
}
console.log(JSON.stringify(this.meetingsArray))
},
return setTimeout(() => { this.newGroup.push(newMeeting) })
This is what you return from a function you call in the emit
, if you want to get the data sent by this function to the event that is captured by the parent component you have to return a value, at this point you either do a setTimeout
before this, and then return the value, but i don't think you need it.这是您从在emit
调用的函数返回的内容,如果您想将此函数发送到由父组件捕获的事件的数据,您必须返回一个值,此时您要么执行setTimeout
在此之前,然后返回值,但我认为您不需要它。 just return the value you want to receive in the parent component, now i have no clue from the context that you gave what you want there, but I am guessing you want to have the newGroup
so you can do a:只需返回您想在父组件中接收的值,现在我不知道您在那里提供了您想要的内容,但我猜您想要拥有newGroup
以便您可以执行以下操作:
this.newGroup.push(newMeeting)
return this.newGroup
This should get you the data in the @updateArray
event.这应该会为您提供@updateArray
事件中的数据。
Next problem, @updateArray="generateGroup2()"
is not correct this actually means/ roughly translates to:下一个问题, @updateArray="generateGroup2()"
不正确,这实际上意味着/大致转换为:
((paramsFromEmit) => generateGroup2())();
It should be: @updateArray="generateGroup2"
, which will mean:它应该是: @updateArray="generateGroup2"
,这意味着:
generateGroup2(paramsFromEmit);
This is what you want called, this will get you the value you emitted in the generateGroup2
function, when the emit triggers this event.这就是您想要调用的,当发出触发此事件时,这将为您提供在generateGroup2
函数中发出的值。 It is pretty hard to understand the subtle difference between these two when you have it written down in the template but I hope this helped you understand it more.当您将它们写在模板中时,很难理解这两者之间的细微差别,但我希望这能帮助您更好地理解它。
Now, I do not really understand what you are trying to do in generateGroup2
, but I will let you figure that out, my answer will get you the data from child to parent, and into generateGroup2
, if you need any more help, feel free to ask.现在,我不太明白您在generateGroup2
中尝试做什么,但我会让您弄清楚,我的回答将使您获得从孩子到父母的数据,并进入generateGroup2
,如果您需要更多帮助,请随时问。 Also, please for the love of all that's holy, don't use setTimeout
s please :D另外,为了所有神圣的事物,请不要使用setTimeout
s :D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.