1

I am migrating things from Vue.js 2 to Vue.js 3. During my migration, I just mentioned that eslint does warn me, because I am mutating props.

This is an example of an element that causes this:

<template>
    <ToggleField
            v-model="item.checked"
            :name="`item.${name}.checked`"/>
</template>
<script>
import ToggleField from "./ToggleField";

export default {
    name: 'TestField',
    components: {ToggleField},
    props: {
        name: {
            type: String,
            required: true,
        },
        item: {
            type: Object,
        },
    },
}
</script>

This element is deeply nested and every parent element passes the :item-attribute to the next "level" until it's finally displayed and changeable due v-model.

This is an example:

Parent view

<template>
  <CustomForm name="test" :item="item" />
  <!-- Reflect changes on item here -->
  {{ item }}
</template>
<script>
import CustomForm from "./CustomForm";

export default {
    components: {
        CustomForm
    },
    data: () => ({
        item: 
          {name: 'Foo', 'checked': false},  
    }),
}
</script>

CustomForm

<template>
         <!-- Do other fancy stuff here -->
    <TestField :name="name" :item="item"/>
</template>
<script>
    import TestField from "./TestField";

    export default {
        name: 'CustomForm',
        components: {TestField},
        props: {
            name: {
                type: String,
                required: true,
            },
            item: {
                type: Object,
            },
        },
    }
</script>

TestField

<template>
    <ToggleField
            v-model="item.checked"
            :name="`item.${name}.checked`"/>
</template>
<script>
import ToggleField from "./ToggleField";

export default {
    name: 'TestField',
    components: {ToggleField},
    props: {
        name: {
            type: String,
            required: true,
        },
        item: {
            type: Object,
        },
    },
}
</script>

So my question is: How can I update the item and reflect the changes to it's parent (and it's parent, and it's parent again, if necessary) without running into the prop-mutation?

SPQRInc
  • 162
  • 4
  • 23
  • 64

0 Answers0