It would be nice for components to have the ability to normalize their properties without having to create an additional computed property with an unwanted property name. Like "myProp" and computed version "myPropNormalized", when the only one I'll ever touch inside the component is "myPropNormalized". "myProp" might as well not exist after it has been normalized.
This would allow components to consume properties in multiple formats for their input but only work off a single format. Easier for both consumers of a component and coders of a component.
I'm not the best API designer, but I suppose it would have to be in the prop validation:
Vue.component('example', {
props: {
myProp: {
type: [Array, String],
transform(value) {
return (typeof value === 'string') ? [value] : value;
}
}
}
});
You are asking for coerce to come back but it was intentionally removed in favour of computed properties in v2: https://github.com/vuejs/vue/issues/2873
That's my problem, computed properties don't replace this functionality. It's creating unwanted aliases when I just need one variable.
Can you point me to the method of manually implementing coerce in Vue 2 via plugins or such as this is essential to my use case?
You may be able to not use a prop and read the value from $attrs but coerce was removed because it was basically a computed property and we realiased it was better to have both values because it's a more explicit approach
Most helpful comment
That's my problem, computed properties don't replace this functionality. It's creating unwanted aliases when I just need one variable.