I am currently working on developing a switch toggle component in Vue that includes a v-model and @updated. However, I am encountering difficulties when trying to update the model once the user toggles the switch. Initially, I faced an issue regarding mutating a prop directly. Now, another error has arisen.
[Vue warn]: Computed property "isSwitchOn" was assigned to but it has no setter.
The intended usage of this component is as follows:
<iswitch v-model="switchGender" @updated="handleUpdatedGender" />
Below is the code for the component itself:
export default {
template: `
<span
@click="toggleSwitch"
:class="{ active: isSwitchOn }">
<span class="toggle-knob"></span>
</span>
`,
props: ['value'],
methods:
{
toggleSwitch()
{
this.isSwitchOn = !this.isSwitchOn
this.$emit('input', this.isSwitchOn)
this.$emit('updated')
}
},
computed:
{
isSwitchOn()
{
return this.value
}
},
};