Performance: Custom v-model on components
This affects how component state updates propagate and how often the DOM updates due to two-way binding.
<CustomInput v-model="inputValue" /> // Inside CustomInput emits 'update:modelValue' event only
<CustomInput v-model="inputValue" @input="val => inputValue = val" />
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual event sync with extra listeners | Multiple reactive updates | 2+ reflows per input | Higher paint cost due to repeated DOM patches | [X] Bad |
| Single 'update:modelValue' event emission | Single reactive update | 1 reflow per input | Lower paint cost with minimal DOM patches | [OK] Good |
v-model?v-model propmodelValue as the default prop name to bind data from parent to child.update:modelValue, matching the prop name.v-model named checked on a component?v-model:customName syntax to bind custom model names.<MyCheckbox v-model:checked="isChecked" /> uses the correct colon syntax for custom v-model binding.props: ['modelValue'],
methods: {
updateValue(val) {
this.$emit('update:modelValue', val)
}
}<ChildComponent v-model="parentData" />
updateValue('new') is called inside the child?update:modelValue with the new value, which Vue listens to for v-model updates.v-model receives the event and updates parentData to 'new'.checked:props: ['checked'],
methods: {
updateChecked(val) {
this.$emit('update:checked', val)
}
}<MyCheckbox v-model:checked="isChecked" />
v-model:checked, the prop must be checked and event update:checked.props: ['checked'] and emits update:checked, which is correct.ToggleSwitch that uses a custom v-model named active. Which of these code snippets correctly implements the child component's props and emits to support this?v-model:active, the prop must be active and event update:active.props: ['active'],
methods: {
toggle() {
this.$emit('update:active', !this.active)
}
} uses props: ['active'] and emits update:active, matching the custom v-model name.Pass the test and this pattern moves unknown