The scenario
Since I have a more complex checkbox I encapsulated it inside a separate component. Inside the template of the parent component I prefer using v-model to bind the value to a variable.
My approach is based on this description (https://v2.vuejs.org/v2/guide/components-custom-events.html#Customizing-Component-v-model) taken from the official documentation.
The problem
When I have two custom-checkbox-elements and I select the last one, the first one inside the DOM will be selected. So it seems, that the first one is consuming the event.
The code
The following snippet illustrates the checkbox component.
<template>
<div class="checkbox-part">
<input class="checkbox-part-input" type="checkbox" name="cb" id="cb"
v-bind:checked="checked"
v-on:change="$emit('change', $event.target.checked)"
>
<label class="checkbox-part-label" for="cb"
:class="{ 'checkbox-part-label--checked': checked }"
>
<slot name="label"></slot>
</label>
<!-- removed for brevetiy -->
</div>
</template>
<script>
export default {
model: {
prop: 'checked',
event: 'change'
},
props: {
checked: {
type: Boolean,
}
}
}
</script>
How can I achieve, that the selected checkbox is updated?
nameandv-modelattribute. Did you make those unique?CheckboxPart. The variable bound tov-modelis called different in both parent components. Does this answer your question?checkedin to the checkbox?