Bind Data to Props with the same Name with shorthand syntax for v-bind="{}" #508
Closed
tikudev
started this conversation in
RFC Discussions
Replies: 1 comment
-
Closed. Problem is fixed with vuejs/core#9451 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
This RFC proposes a shorthand syntax in templates for v-binding entire objects and to allow multiple object-v-binds on the same element. Those seemingly unrelated features could help resolve a long standing issue (see Reference Issues) in the vue community. That is it would allow to express prop bindings with the same name, without writing the name twice and without much more boilerplate.
Basic example
Motivation
Improve readability and reduce verbosity in Vue.js templates when binding a property to a variable with the same name and to do so in a manner that integrates well with the existing syntax.
Detailed design
Terminology: I will call a binding without naming a prop (
v-bind="Obj"
) an object-v-bindProblem
A lot of people in the community expressed their feeling, that the binding of a property to a variable with the same name seems too verbose (see Reference Issues). On those occasions, one proposed solution to this problem, was to just use an object-v-bind, create an object and use the js object property shorthand (
v-bind="{foo}"
). This solution has the following drawbacks:1. verbosity
For short variable names, this solution could even introduce more characters
2. forced grouping
All props that use this shorthand need to be put together inside the same v-bind
Solution
1. Verbosity: Shorthand for object-v-bind
v-bind="{foo}"
->{foo}
v-bind="{foo, bar}"
->{foo, bar}
Should be possible to implement, since
<foo {abc}/>
is not valid html. Vite fails with:Uncaught DOMException: Failed to execute 'setAttribute' on 'Element': '{abc}' is not a valid attribute name.
2. Forced Grouping: Allow multiple object-v-binds
Right now this is not possible (and so verbose, that I think that no one seriously tried it, to solve a problem):
->
[vite] Internal server error: Duplicate attribute
My mental model for object-v-binds is, that an object is 'spread' into a component, therefore I did at first not expect this to be invalid, and it does not need to be in my opinion. The same reason for why
const myComponent = {...{width}, ...{height}}
is valid javascript.Edge Case: Property Merging with multiple v-binds
Vue 3 specific
Coincidental Similarities to Svelte Prop shorthand
In Svelte the syntax for binding a prop is a little different:
In Svelte it is determined whether the content of an attribute is an expression through curly brackets around the content. In vue it is determined through the colon in front of the attributes name.
The shorthand in Svelte looks like this:
Thus the 'expression-defining' characters are placed around the prop.
In Vue this would look like this:
However, this proposal is not primarily about this
:prop
syntax, and is about doing it differently than Svelte by using the same syntax as Svelte:Drawbacks
{a, b, c}
or{a} {b} {c}
or{a, b} {c}
? Should I usev-bind="$attrs"
or{...$attrs}
?{}
is the shorthand vorv-bind="{}"
". It is still new syntax, that needs to be learned.:prop
shorthand (roughly every six months, see Reference Issues)Alternatives
The big elephant in this RFC are the existing 16 times, that someone from the community tried to tackle this problem, since May, 2016. The proposed syntax primarily was
:prop
. This syntax seems to have problems. However, it seems that this syntax might still be the most intuitive, since it had been reinvented for most of the related issues.Another alternative is of course to not invent anything new. To me it seems, through the amount of issues each year, that this problem should be addressed.
Adoption strategy
This proposal can be adopted in a similar way as other shorthands (v-slot
#
shorthand;v-bind:prop=""
->:prop
).One could configure the desired style in a linter and have the linter auto-fix an existing Code-Base.
Unresolved questions
What were the reasons to previously not allow multiple object-v-binds? This is maybe not as simple as it seems.
Does the introduction of this new syntax block capabilities of formulating a different syntax in the future? Is it worth it?
Beta Was this translation helpful? Give feedback.
All reactions