Please see this minimum example
App.vue
<template>
<EditPerseon :previousPerson="persons[0]" />
</template>
<script>
import EditPerseon from "./EditPerseon.vue";
export default {
data() {
return {
persons: [
{ name: "Bob", age: 18 },
{ name: "Amy", age: 20 },
],
};
},
watch: {
$data: {
handler() {
console.log("My $data have been mutated!");
},
deep: true,
},
},
components: {
EditPerseon,
},
};
</script>
EditPerseon.vue
<template>
<input v-model="currentPerson.name" />
</template>
<script>
export default {
props: {
previousPerson: Object,
},
data() {
return {
currentPerson: this.previousPerson,
};
},
};
</script>
Also on CodeSandbox:
EditPerseon.vue is my child component, but it still accidentally mutate my parent's $data.
Why is this happening?
How can I prevent this?