Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue 3 V-Model Passing to Grandchild Component

I have three components that I would like to pass a reactive model down through from the parent -> child -> grandchild (vee-validate Field).

So parent component would look like:

<template>
  <child v-model="formData" />
</template>
.
.
.
setup() {
  const formData = ref<CreateAccount>({
      email: "",
      firstName: "",
      lastName: ""
  });

  return {
    formData,
  };
}

Child component (with grandchild component) looks like:

<template>
  <Field
    type="text"
    name="email"
    v-model="modelValue.email" ????
  />
</template>

export default defineComponent({
    name: "step-2",
    components: {
      Field,
    },
    props: {
      modelValue: {
        type: Object,
        required: true,
      },
    },
    emits: ["update:modelValue"],
  },
});

Now my issue is that I cannot just pass modelValue to the Field v-model prop, so I'm not sure if there is a chain of events or restructuring the child modelValue that needs to be done?

like image 657
Ben Krueger Avatar asked Oct 14 '25 21:10

Ben Krueger


1 Answers

I ended up using the following solution in my child component:

<template>
  <Field
    type="text"
    name="email"
    v-model="model.email"
  />
</template>

export default defineComponent({
  name: "step-2",
  components: {
    Field,
  },
  props: {
    modelValue: {
      type: Object,
      required: true,
    },
  },
  computed: {
    model: {
      get() {
        return this.modelValue;
      },
      set(value) {
        this.$emit("update:modelValue", value);
      },
    },
  },
},
});
like image 64
Ben Krueger Avatar answered Oct 17 '25 12:10

Ben Krueger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!