Performance: v-model with radio buttons
This affects how user input updates the UI and how Vue manages DOM updates for radio button groups.
<template> <input type="radio" value="option1" v-model="selected" /> Option 1 <input type="radio" value="option2" v-model="selected" /> Option 2 </template> <script setup> import { ref } from 'vue' const selected = ref(null) </script>
<template> <input type="radio" value="option1" :checked="selected === 'option1'" @change="selected = $event.target.value" /> Option 1 <input type="radio" value="option2" :checked="selected === 'option2'" @change="selected = $event.target.value" /> Option 2 </template> <script setup> import { ref } from 'vue' const selected = ref(null) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual @change handlers on each radio | Multiple event listeners | Multiple reflows if state triggers broad updates | Higher paint cost due to extra layout recalculations | [X] Bad |
| v-model binding on radio buttons | Single reactive binding per input | Minimal reflows limited to changed input | Lower paint cost due to targeted updates | [OK] Good |
v-model do when used with radio buttons in Vue?v-model directive in Vue binds form inputs to a data property, syncing the input's value with the data.v-model binds the selected button's value to a single data property, so only one value is stored.v-model to a data property named choice?v-model, not v-bind or :model.value attribute sets the radio's value, and v-model='choice' binds the selected value to the choice data property.<template>
<input type='radio' id='red' v-model='color' value='red'>
<label for='red'>Red</label>
<input type='radio' id='blue' v-model='color' value='blue'>
<label for='blue'>Blue</label>
<p>Selected color: {{ color }}</p>
</template>value is assigned to the bound data property, here color.value='blue', so color becomes 'blue'. The paragraph shows this value.<input type='radio' v-model='selection' value='A'> <input type='radio' v-model='selection' value='B'> <input type='radio' v-model='selection' value='C'>
v-model binds to a data property. If selection is undefined initially, no radio is selected by default.selection to that value, but if the component does not react or update, it may be due to missing reactive data or errors elsewhere.selection is not initialized in the Vue component. -> Option BselectedFruit with options 'Apple', 'Banana', and 'Cherry'. You also want the label text to be clickable for accessibility. Which of the following Vue template snippets correctly implements this?input inside a label makes the label text clickable and improves accessibility without needing for and id.<div>
<label>
<input type='radio' v-model='selectedFruit' value='Apple'> Apple
</label>
<label>
<input type='radio' v-model='selectedFruit' value='Banana'> Banana
</label>
<label>
<input type='radio' v-model='selectedFruit' value='Cherry'> Cherry
</label>
</div> wraps each radio input inside a label with the text, which is the recommended accessible pattern.Pass the test and this pattern moves unknown