Replies: 4 comments 2 replies
-
+1 from https://stackoverflow.com/q/77600199/19171888 Perhaps a compiler option could be added to control this behavior |
Beta Was this translation helpful? Give feedback.
-
It's not prohibited <template>{{ foo.ref.value }}</template>
<script setup>
const foo = {
ref: ref('blah')
}
</script> |
Beta Was this translation helpful? Give feedback.
-
It's not prohibited. Top-level refs are unwrapped, meaning any refs that are at component/module scope acts as if you had included Personally, I find the unwrapping makes things very succinct most of the time, but anything slightly complex gets confusing as you have to keep the unwrapping in mind. In one way, this is good: as a rule of thumb, if your code is confusing you're probably going about it the wrong way. However, this applies to any nested object, so even Unfortunately, there are so many edge cases and considerations. And so we come the full circle: unwrap top level refs, as that is what you usually are working with. If you have a situation where it would make things simpler, there is a workaround. You can use reactive objects instead of objects of refs, as reactive objects do not use |
Beta Was this translation helpful? Give feedback.
-
All very good and very insightful and interesting replies in the thread. Thank you very much, guys! Now, excuse my ignorance/naivety but why is it necessary to have autounwrap at all if it causes so many exceptions in the syntax? I would much rather have no autounwrap whatsoever and have a consistent syntax instead. It's much easier to remember adding .value after any reactive item in the code, no matter the place in the code, than to remember all the rules connected to the autounwrap. It is currently day 3 of my really intensive (6-7 hours a day) studying of how reactivity in Vue works and I feel like it's not really justified. I simply don't see the value in autounwrap at all. Somewhat shorter code? Not enough in my opinion considering the tradeoff. But as I said I am a beginner in Vue so excuse me if I miss more important context connected to the wrapping logic. Feel free to put me in my place. I thank you very much for your time and know-how. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
As the title suggests I would actually prefer to use '.value' properties in templates too as that would unify the syntax between the script section and the template section in the code.
Reading the official docs at first I actually got the impression that omitting the '.value' in the template section with Composition API was optional (see the quote below*). Now I know that wasn't the case and we simply cannot use .value syntax in the template section. The docs say that the automatic unwrapping of refs in templates is a convenience feature. I beg to differ. I think it causes unnecessary inconsistency in the syntax and makes it more difficult for beginners to learn it (I am a beginner in Vue).
I would also propose to change the wording in the docs so that the reader immediately understands that using .value in the template section is explicitly prohibited. The wording should be something like "You can not use .value when using refs in the template.".
*The quote:
"Notice that we did not need to append .value when using the ref in the template. For convenience, refs are automatically unwrapped when used inside templates (with a few caveats)."
Beta Was this translation helpful? Give feedback.
All reactions