1👍
✅
When setting v-model on a custom component you must declare a prop in the child called modelValue
. Then, following the Vue documentation:
Bind the
value
attribute of a native<input>
element to themodelValue
propWhen a native
input
event is triggered, emit anupdate:modelValue
custom event with the new value
Select.vue
<template>
<div class="select" inheritAttrs="false">
<select
v-bind="$attrs"
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)">
<slot></slot>
</select>
<!-- ... -->
</div>
</template>
<script>
export default {
props: {
modelValue: {
type: String,
default: ''
}
}
}
</script>
Besides doing all that, you also had one mistake in your <option>
v-for where you set :value="options"
, where options
is the entire array of values, when it should be :value="option"
which assigns a single string value.
<option v-for="option in options" :key="options" :value="option">
Source:stackexchange.com