search

Home  >  Q&A  >  body text

Vue 3 V model passed to grandchild component

I have three components and I want to pass a reactive model down from parent -> child -> grandchild (vee-validate field).

So the parent component looks like:

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

  return {
    formData,
  };
}

Child components (with grandchildren) look 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 problem is that I can't just pass the modelValue to the Field v-model property, so I'm not sure if there is a series of events or...

P粉731861241P粉731861241241 days ago501

reply all(1)I'll reply

  • P粉044526217

    P粉0445262172024-03-27 00:32:00

    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);
          },
        },
      },
    },
    });

    reply
    0
  • Cancelreply