How to correctly write code with props?

Hello!

Read docks on vue says that it is not correct to change the values of the props and it gives the GTA V on garage door. When in the beginning he told me, and alert. But the code below it does not swear. In fact I have one component passes the object to another component, and then write the code.

computed: {
 fields () {
 for (var key in this.data) {
 if ( ! this.data[key]['attr'] ) this.data[key]['attr'] = {};
 if ( ! this.data[key]['attr']['id'] ){
 this.data[key]['attr']['id'] = 'Forms_'+this.data[key]['name'];
}
}
 return this.data;
}
 }


In this case, not so bad that change properties of the parent object, but ideally it would be nice in the future to write more correct code. Tell me how would be correct?
June 10th 19 at 14:51
1 answer
June 10th 19 at 14:53
Solution
Need to keep the prop in data () if you plan to change it.
props: {
 value: [String, Number, Object, Array]
}
data() {
 return {
 mutableValue: this.value
};
}


Further mutableValue twist anything. More correct would be to keep the prop value through the watch.
But in this case mutableValue variable will contain an object reference or is there some clever mechanism?
Show if not difficult to watch an example... - Armand_Nader93 commented on June 10th 19 at 14:56
, Look at this component: https://github.com/sagalbot/vue-select/blob/master... - pauline.Herman commented on June 10th 19 at 14:59

Find more questions by tags Vue.js