-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a way to define different name for prop attribute #7943
Comments
I'm interested in this feature too. Is this feature confirmed to be implemented? I'm willing to work on this. Edit: I'm currently now working on this, please feel free to jump in. |
Besides, there's also another enhancement about Here's a simple code. props:{
size: {
type: Number,
}
} And here's a parent component: <parent>
<child size="1"></child>
</parent> We know it's a wrong usage, but I think, now that we provided the field After seeing this issue, I think maybe my choice can be a default function of his |
Regarding the transform, it's basically |
@posva, I've updated FR description. |
To give this some life again, I am also very much interested regarding this feature. I would though call it props: {
rawSize: {
propName: 'size',
type: Number,
default: 1,
}
} |
Here is a Vue mixin (fjc0k/vue-messenger) including a series of useful enhancements to Vue components props:
Hope this helps. |
It's been a while, but I'm going to bring it up again, what's the status of this feature request? Is it confirmed or already implemented? The proposed (fjc0k/vue-messenger) does not solve the issue. Again, we simply want an alias to prop names, so we wouldn't have to write I'm willing to work on this feature, is it confirmed/implemented? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
It seems to me that this would be preferable: props: {
size: {
type: String,
alias: "rawSize"
}
} Rather than this: props: {
rawSize: {
type: String,
propName: "size"
}
} The external facing prop name should remain as the key in the Here is an example of an existing Original proposal: props: {
size: {
type: String
}
} gets changed to: props: {
rawSize: {
type: String,
propName: "size"
}
} My suggestion: props: {
size: {
type: String
}
} gets added to: props: {
size: {
type: String,
alias: "rawSize"
}
} |
Edit: Oops, I made a mistake, I was intending to reply to vuejs/rfcs#10. But it's still valid here. I'll reply in there too. I think the examples list in the Motivation section of the RFC (as well as some use cases commented) are not good arguments for this proposal.
props: ['initialCounter'],
data() {
return {
counter: this.initialCounter
}
}
props: ['size'],
computed: {
normalizedSize() {
return this.size.trim().toLowerCase()
}
} With the introduction of Composition API, those use cases could be handled elegantly: props: ['size'],
setup(props) {
const size = computed(() => {
return props.size.trim().toLowerCase()
})
return {
size
}
} That being said, I'm not entirely against this. The Swift example in the RFC regarding argument labels is what in the similar vein with this proposal. props: {
person: String,
from: {
as: 'hometown',
type: String
}
} And take the example of the new props: {
to: {
type: [String, Element]
}
},
setup(props) {
const target = toRef(props, 'to')
// setting up
return {
target
}
} But it's not as expressive and cohesive as following IMO: props: {
to: {
as: 'target',
type: [String, Element]
}
},
setup(props) {
// setting up
return {
//
}
} So, in summary:
|
This is a cool feature. I think the idea is similar to that of how Swift allows you to have an internal variable and external parameter name. (https://useyourloaf.com/blog/swift-named-parameters/) Sometimes it makes sense to have verbose external names for clarity, and shorter names inside the component. For example, a component can have a long name like this: <MyTable v-bind:userDataFromSomeAPI="data" /> but inside the component I don't want to refer to it as So I would really like this! I think |
Another use case for such alias is having components created with |
Something like
|
This comment has been minimized.
This comment has been minimized.
Any progress? would be a nice to have...! as a workaround: (in the meantime) add a new prop for every alias, and then using computed value ... but it adds more lines to the component (compared to a simple one line as @hrobertson suggested with: |
This is my use case. Currently what I do isn't terrible, just looking for more elegance and eliminating one extra variable:
With 20 components or so, and inside any given component pass prop payload into a const. Example: Customer component:
Which allows me to use |
What problem does this feature solve?
In most of cases, it's not really comfortable to use
initialProp
as prop name, for example, or havenormalizedProp
inside a component, which takes some passed prop and transforms it. Code looks bloated and reminds more workaround than a good solution.Having ability to change attribute name of prop would be great. Something like:
What does the proposed API look like?
Thus, using any of proposed solutions above,
this.size
inside component would return transformed value (for example,h4
or just4
).I believe having this feature would be very awesome and help us to write cleaner code.
The text was updated successfully, but these errors were encountered: