vuejs/rfcs

Adding a .v shortcut for .value

tintin10q opened this issue · 4 comments

What problem does this feature solve?

To access the value of a ref() you use .value. While .value is very explicit it is maybe also too long? I suggest adding a .v shortcut for .value.

The use case for .v is the same as .value but just a neat shortcut. I think this will improve the end user experience because you have to do less typing. Shortcuts can make using a framework more enjoyable just like the : shortcut for v-bind and @ for v-on.

I also think it is nice because the framework is called Vue and then access the .v attribute the whole time. But it should also make bundle sizes smaller because .v is 5 times as less text than .value and this might make a difference if you have a large project.

I think .v still makes it as clear what you are doing as .value but it is just much faster and nicer to type.

What does the proposed API look like?

Here is are two examples:

<script setup>
import {ref} from 'vue';

const counter = ref(3);

function increment() {
  counter.v++;  
}

</script>

You could also mix them:

<script setup>
import {ref} from 'vue';

const counter = ref(1);
const counter2 = ref(2);

function increment() {
  counter.v++ 
  counter2.value++
}

</script>

I've actually thought about this, but .v would look too cryptic IMO.

Something like .val could be short but still legible.

It's not more cryptic than $$ stuff from reactivity transform
I personally don't mind .value, but I was thinking about $ for the replacement of value to make it short and somehow similar to reactivity transform. See the example, both are for me similarly cryptic:

// reactivity transform
let count = $ref(0)
trackChange($$(count))

// .value replacement
const count = ref(0);
doSomething(count.$)

Another reason is that it's easily searchable (searching for .v may find much more results than you'd expect while searching for .$ should be more accurate)

I've actually thought about this, but .v would look too cryptic IMO.

Something like .val could be short but still legible.

That is why it should be a shortcut. .value is not cryptic and then .v is the shortcut. Just like v-on is not cryptic and than @ is the shortcut.

duck不必