I'm trying to make a wrapper component for an <input/> element in Vue.js.
Component:
<template>
<div>
<input v-bind="$attrs" :value="value" @input="input" />
...
</div>
<template>
Vue.component("my-input", {
inheritAttrs: false,
props: ['value'],
methods: {
input($event): void {
this.$emit("input", $event.target.value)
}
}
})
Usage:
<my-input v-model="myModel" />
This seems to work just fine. The model gets updated via the input event handler by emitting the target element value.
However, now I'm trying to use this component with some existing code:
<my-input v-model="myModel2" @input="something = $event.target.value" />
This is where I'm having trouble with the $emit("input") event. I'm getting the following error:
Cannot read property 'value' of undefined
So, my $emit is emitting the value and now the existing @input="something..." event handler can't reference the $event properly.
If I change my component's input method to emit the $event instead of $event.target.value, the new code seems to work, but then model doesn't get updated gets updated to the InputEvent instead of the actual value.
I'm not sure what I need to do.