I’m attempting to use the Tailwind headlessui <Switch>
component in a Vue3/InertiaJS app, and when I put it in my component as it is in the docs, using an enabled ref()
variable for the value, it works fine. However, when I bind it via v-model
to a useForm()
value like so:
<script setup>
const form = useForm({
enabled: false
...
})
</script>
<template>
<Switch
v-model="form.enabled"
...
>
</template>
it doesn’t work. Instead, the button doesn’t work at all, and there are no console errors. I have many other fields in the same form that work fine with the form fields, so its’ just this one. The only difference I can see is that the examples use ref()
, and useForm()
uses reactive()
, but I can’t see why that would make a difference.
2
Answers
Credit to the answer above by @mikalai-parakhnevich, I was missing the form reference for the
:class
binding. I did not need the@update:modelValue
line.At first glance, there is no error in your description of the problem. I’ll give an approximate usage algorithm, maybe it will help
Install inertiajs for vue
Import
useForm
from inertiajs in your componentCreate a form object:
We use it in the component template as:
Full code:
and an example in action