Computed property was assigned to but it has no setter - a toggle component
The error is triggered by this statement: this.isSwitchOn = !this.isSwitchOn
. You are trying to assign a value to a computed property but you didn't provide a setter
.
You need to define your computed property as follow for it to work as a getter
and a setter
:
computed:
{
isSwitchOn:
{
get()
{
return this.value
},
set(value)
{
this.value = value
}
}
}
Also, it is not advised to mutate a prop directly. What you could do is to add a new data property and sync it with the value
prop using a watcher.
I think something like this will work:
props: ['value'],
data()
{
return {
val: null
}
},
computed:
{
isSwitchOn:
{
get()
{
return this.val
},
set(value)
{
this.val = value
}
}
},
watch: {
value(newVal) {
this.val = newVal
}
}