In order to properly copy props to local data and manipulate them
First, in your example you have no local data. What your computed property is is some temporary object, which can be recreated any time something changes. Don't do that.
If the prop is really just some initial value, you can introduce property in data()
and initialize it right there from prop
If you are passing the prop to component in order to change its value, you probably want that changed value passed back to your parent. In that case you don't need to copy anything. Just props down, events up (either with v-model, .sync
or just handling events manually)
In the case Object is passed by prop, you can also directly mutate object's properties - as long as you don't change prop itself (swap it for different object), everything is fine - Vue will not throw any warning and your parent state will be mutated directly
UPDATE
Now I have better understanding of use case. Question should be more like "Best way to pass data into a modal popup dialog for editing item in the list/table"
- Dialog is modal
- Dialog can be shown for specific item in a table by clicking button in it's row
- Dialog is cancelable (unless the user uses Save button, any changes made in the dialog should be discarded)
In this particular case I don't recommend using props to pass the data. Props are good for passing reactive values "down". Here you don't need or want dialog to react for data changes. And you also don't want use props for 1-time initialization. What you want is to copy data repeatedly at particular time (opening the dialog).
In component with table (row rendering):
<td>
<v-btn rounded @click="openDialog(item)">open details</v-btn>
</td>
Place dialog component outside the table:
<person-form @update="onUpdate" ref="dialog"></person-form>
In methods:
openDialog(item) {
this.$refs.dialog.openDialog(item);
},
onUpdate(item) {
// replace old item in your data with new item
}
In dialog component:
openDialog(item) {
this.form = { ...item }; // copy into data for editing
// show dialog....
},
// Save button handler
onSave() {
this.$emit("update", this.form);
// hide dialog...
}
Demo