Vue 3 V model passed to grandchild component
P粉731861241
P粉731861241 2024-03-26 15:37:53
0
1
544

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粉731861241
P粉731861241

reply all(1)
P粉044526217

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);
      },
    },
  },
},
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template