You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<scriptsetup>import{reactive}from'vue';import{useField,useForm,useFieldArray}from'vee-validate';constform=reactive(useForm());constfield=reactive(useField('field'));constfieldArray=reactive(useFieldArray('users'));</script><template><!-- ❌ Still Doesn't work because it is a writeable computed ref --><inputv-model="field.value" /><!-- ✅ Works if unwrapped manually --><inputv-model="field.value.value" /><!-- ✅ Works --><pre>{{ form.meta.valid }}</pre><!-- ✅ Works --><divv-for="item in fieldArray.fields"></div></template>
I try to reproduce it and made an example myself, but my version is working. I made a function return a plain object that contains a property value which is a writable computed ref. I don't need to unwrap myField.value like myField.value.value in the template.
<scriptsetup>import{computed,reactive,ref}from'vue'functiontest(){constfield=ref('')constvalue=computed({get(){returnfield.value},set(v){field.value=v},})return{ value }}constmyField=reactive(test())</script><template><p>
myField.value
{{ myField.value }}
<inputv-model="myField.value" type="text"></p></template>
The reactive conversion is "deep": it affects all nested properties. A reactive object also deeply unwraps any properties that are refs while maintaining reactivity.
I thought it means that after we transform an object with reactive(), all refs are unwrapped. I don't use reactive() on a daily basis, but I wonder if this behavior is something I should be aware of.
I found something interesting. If we make shallow copy of useField() then pass it to the reactive, then the computed ref is unwrapped automatically in the template.
<scriptsetup>import{reactive}from'vue';import{useField}from'vee-validate';// transform plain object from `useField()` to reactive consta=reactive(useField('a'))// make a shallow copy of useField() before passing to `reactive()`constb=reactive({ ...useField('b')})</script><template><!-- This doesn't work --><p>
{{ a.value }}
<inputv-model="a.value" type="text"></p><!-- But this work!--><p>
{{ b.value }}
<inputv-model="b.value" type="text"></p></template>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I see this example at the bottom in the document https://vee-validate.logaretm.com/v4/guide/composition-api/caveats/#destructing-composable
I don't understand why do we need to unwrap
field.value
manually in the template.I try to reproduce it and made an example myself, but my version is working. I made a function return a plain object that contains a property
value
which is a writable computed ref. I don't need to unwrapmyField.value
likemyField.value.value
in the template.In the Vue 3 document, it says
I thought it means that after we transform an object with
reactive()
, all refs are unwrapped. I don't usereactive()
on a daily basis, but I wonder if this behavior is something I should be aware of.I found something interesting. If we make shallow copy of
useField()
then pass it to thereactive
, then the computed ref is unwrapped automatically in the template.Beta Was this translation helpful? Give feedback.
All reactions