Answer the question
In order to leave comments, you need to log in
How to get selected value of same type in Select Vue component?
There is a wrapper component for select:
<template>
<select v-on="listeners" :value="value">
<option v-for="(option, index) of list" :key="index" :value="option">{{ option }}</option>
</select>
</template>
<script>
export default {
name: "x-select",
props: {
value: {
type: [Number, String, Boolean],
required: false,
default: ""
},
list: {
type: Array,
required: false,
default: () => []
}
},
computed: {
listeners() {
return {
...this.$listeners,
input: ({ target: { value } }) => this.$emit("input", value)
};
}
}
};
</script>
list
? So, for example, native select works, if you do not use it as a component, how to achieve this effect, where to get the value for $emit from? <template>
<x-select v-model="selected" :list="list" />
</template>
<script>
export default {
name: "app",
data: () => ({
list: [1, true, "124.5", false, 15.7],
selected: 1
})
};
</script>
<template>
<select v-model="selected">
<option v-for="(option, index) of list" :key="index" :value="option">
{{ option }}
</option>
</select>
</template>
<script>
export default {
name: "app",
data: () => ({
list: [1, true, "124.5", false, 15.7],
selected: 1
})
};
</script>
selected
will contain exactly the value that is in the array list
with the same type. Tell me how to achieve such a result? Is it possible to parse yourself? Answer the question
In order to leave comments, you need to log in
Use not the values themselves as option values, but their indexes inside the list array. It will be necessary to emit, respectively, list[target.value].
https://jsfiddle.net/krnLyf2q/
Or make a property with a setter inside the computed component (in which $emit will be executed) and use it in v-model.
https://jsfiddle.net/krnLyf2q/1/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question