Reputation: 3
How to catch an emeit event.target with vue ? Trying to make an $emit vue and getting an error
I tryed to catch an event target emit from a child component from an input , passing as a param the onChange input string. But getting an error "Cannot read properties of undefined (reading 'target')". Even though the syntaxis is correct I assume.
<input
type="text"
placeholder="Search here"
v-model="search"
:change="$emit('search', $event.target.value)"
// Parent
<Search @search="searchProduct" />
<script>
methods: {
searchProduct(searchInput) {
this.search = searchInput;
},
},
/>
Upvotes: 0
Views: 522
Reputation: 1387
To listen for events in Vue, we use v-on:event-name="..."
syntax. It can also be shortened to @event-name="..."
. You can find more details here.
You are mistakenly listening for :change
instead of @change
. So update code should be:
<input
...
@change="$emit('search', $event.target.value)"
|__ `@` instead of `:`
/>
Upvotes: 2