vuejs / rfcs

RFCs for substantial changes / feature additions to Vue core
4.87k stars 548 forks source link

Adding a .v shortcut for .value #440

Closed tintin10q closed 2 years ago

tintin10q commented 2 years ago

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>
yyx990803 commented 2 years ago

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

Something like .val could be short but still legible.

jacekkarczmarczyk commented 2 years ago

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)

tintin10q commented 2 years ago

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.

zhenzhenChange commented 2 years ago

duck不必