Answer the question
In order to leave comments, you need to log in
How to make a radio button component in Vue?
Hello. I saw an example of working with radio buttons in Vuetify:
<template>
<v-container
class="px-0"
fluid
>
<v-radio-group v-model="radioGroup">
<v-radio
v-for="n in 3"
:key="n"
:label="`Radio ${n}`"
:value="n"
></v-radio>
</v-radio-group>
</v-container>
</template>
<script>
export default {
data () {
return {
radioGroup: 1,
}
},
}
</script>
v-radio-group
somehow tracks v-radio
internally and returns the selected option. Actually, the question arose how to make the same behavior, instead of several v-radio
with v-model for one variable?
Answer the question
In order to leave comments, you need to log in
props: [ 'value' ],
provide() {
return {
radioGroup: this,
};
},
props: [ 'value' ],
inject: [ 'radioGroup' ],
<input
type="radio"
:value="value"
:checked="radioGroup.value === value"
@change="radioGroup.$emit('input', value)"
>
props: [ 'modelValue' ],
emits: [ 'update:modelValue' ],
setup(props, { emit }) {
provide('radioGroupValue', computed({
get: () => props.modelValue,
set: v => emit('update:modelValue', v),
}));
},
props: [ 'value' ],
setup() {
return {
radioGroupValue: inject('radioGroupValue'),
};
},
<input
type="radio"
:value="value"
v-model="radioGroupValue"
>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question