I'm creating a to-do list in vue.js and the following piece of code in the child element returns undefined
<input v-model="titleText" type='text'>
<input v-model="projectText" type='text'>
<button class='ui basic blue button' v-on:click="sendForm()">
Create
</button>
<button class='ui basic red button' v-on:click="closeForm()">
Cancel
</button>
sendForm () {
if (this.titleText.length > 0 && this.projectText.length > 0) {
this.$emit('create-todo', {
title: this.titleText,
project: this.projectText,
done: false
})
}
this.titleText = ''
this.projectText = ''
this.isCreating = false
}
}
Parent element:
<template>
<div id="app">
<todo-list v-bind:todos="todos"></todo-list>
<create-todo v-on:create-todo="createTodo()"></create-todo>
</div>
</template>
methods: {
createTodo (newTodo) {
console.log(newTodo)
this.todos.push(newTodo)
}
}
v-on:create-todo="createTodo()"instead ofv-on:create-todo="createTodo"orv-on:create-todo="createTodo(foo, bar, ...arguments)".createTodo()method, which could easily be causing your problem as the emitted data would simply be ignored. In the second, the arguments will be passed in as expected. In the third, we pass infooandbaras the first and second arguments respectively, then pass in the JavaScript...argumentsobject which will unpack all additional arguments emitted by thecreate-todoevent separately--that is, in this case, your emitted data would become the third argument.