Vue: Transform data when properties are set on a component.

Created on 15 Feb 2018  路  4Comments  路  Source: vuejs/vue

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;
      }
    }
  }
});

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.

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franciscolourenco picture franciscolourenco  路  3Comments

seemsindie picture seemsindie  路  3Comments

paceband picture paceband  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments

6pm picture 6pm  路  3Comments