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

Prevent errors in existing cases binding props with v-model #11029

Open
cnotv opened this issue May 14, 2024 · 1 comment · May be fixed by #11045
Open

Prevent errors in existing cases binding props with v-model #11029

cnotv opened this issue May 14, 2024 · 1 comment · May be fixed by #11045
Assignees
Labels
Milestone

Comments

@cnotv
Copy link
Contributor

cnotv commented May 14, 2024

Description

Investigate issues printed out by the Vue3 builder about the use of props in v-model.

Context

Since Vue3, it is not possible to rebind props with v-model. Compiler error:

VueCompilerError: v-model cannot be used on a prop, because local prop bindings are not writable.
Use a v-bind binding combined with a v-on listener that emits update:x event instead.

The error is generated from vue/core and there are at the moment 54 occurrences.
Specifically to this codebase, it happens mostly when we define in the markup v-model="value", at the moment 86 occurrences.

E.g.:

Screenshot 2024-05-14 at 20 16 19

Please note:

  • In Vue2 props bound with v-model are defined as value, while in Vue3 as modelValue
  • The error is generated exclusively on reassignment, so do not confuse the term "prop mutation" with "object mutation"

Solution

In the current phase, the only solution seems to replace the prop with a computed property. This can be automated and solved by using a mixin that would add prop modelValue and computed value, then remove the prop value (example PR).

Using existing migration script, we would need to remove all the occurrences where we define value as a prop, including mixins and components in .js format.

References

Collaterals

As in Vue3 the v-model binding is generating a modelValue instead of value, we'll need to apply this mixing everywhere.

@cnotv cnotv added the kind/tech-debt Technical debt label May 14, 2024
@cnotv cnotv added this to the v2.9.0 milestone May 14, 2024
@cnotv
Copy link
Contributor Author

cnotv commented May 16, 2024

Crossing an issue using the mixin, due to the non-matching multiple cases.
Example cases of values props to be replaced:

props: {
  value: {
    type:     Object,
    required: true,
  },
  value: {
    type:    Array,
    default: null,
  },
  value: {
    type:    [Array, Object],
    default: null,
  },
  value: {
    type:    String,
    default: ''
  },
  value: {
    type:    [Array, Object],
    default: null,
  },
  value: {
    type:    Object,
    default: () => {
      return {};
    }
  },
  value: {
    type:    Object as PropType<Badge>,
    default: null
  },
  value: {
    type:    [Boolean, Array, String] as PropType<boolean | boolean[] | string>,
    default: false
  },
  value: {
    required:  true,
    validator: () => true
  },
  value: {
    type:     Number,
    required: true,
    validator(value) {
      return value >= 0;
    }
  },
  value: {
    default: null,
    type:    [String, Object, Number, Array]
  },
}

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

Successfully merging a pull request may close this issue.

3 participants