Skip to content
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

Transform data when properties are set on a component. #7657

Closed
Giwayume opened this issue Feb 15, 2018 · 4 comments
Closed

Transform data when properties are set on a component. #7657

Giwayume opened this issue Feb 15, 2018 · 4 comments

Comments

@Giwayume
Copy link

What problem does this feature solve?

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.

What does the proposed API look like?

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;
      }
    }
  }
});
@posva
Copy link
Member

posva commented Feb 15, 2018

You are asking for coerce to come back but it was intentionally removed in favour of computed properties in v2: #2873

@posva posva closed this as completed Feb 15, 2018
@Giwayume
Copy link
Author

That's my problem, computed properties don't replace this functionality. It's creating unwanted aliases when I just need one variable.

@Giwayume
Copy link
Author

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?

@posva
Copy link
Member

posva commented Feb 16, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants