C
C
Chemnan2021-06-01 14:16:27
Vue.js
Chemnan, 2021-06-01 14:16:27

How to solve the problem with props mutation in a component?

I get the value prop into the component, then it changes (when editing the input, for example), I solved this problem through computed, everything works correctly, but how can I do it so as not to write the set section? Nothing happens in it and it is not needed, but if you delete it, then Vue swears at the lack of a setter:

selectedValue: {
      get: function () {
        return this.value;
      },
      set: function () {}
 }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-01
@Chemnan

Rename value to initialValue (this means that the name reflects the essence more accurately), make selectedValue a normal property from the calculated property:

data() {
  return {
    selectedValue: this.initialValue,
  };
},

Well, or think again about how you are going to use this component - maybe it should be able to work with the v-model directive? Then rewrite the setter like this:
set(val) {
  this.$emit('input', val);
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question