I am relatively new to Vue and working on my first project. I'm trying to create a form with multiple child and grandchild components. I've run into a problem where I need to be able to generate multiple copies of a form. Therefore, I moved some data attributes up 1 level. Currently, the form is ApplicationPage.Vue > TheApplication.Vue > PersonalInformation.Vue > BaseInput.Vue. My problem is that I need to emit changes from PersonalInformation to ApplicationPage via TheApplication . I'm having a hard time figuring out how to handle this situation. I've been looking for a solution for Vue2 but haven't found a solution for Vue3.
ApplicationPage.vue
templatescript data() { return { data: { primary: { personalInformation: { first_name: "", middle_name: "", last_name: "", date_of_birth: "", phone: null, email: "", pets: "", driver_license: null, driver_license_state: "", number_of_pets: null, additional_comments: "" }, }, }, } },
TheApplication.Vue
methods: { UpdateField(field, value) { this.$emit('update:modelValue', {...this.modelValue, [field]: value}) },
Personal information.vue
methods: { onInput(field, value) { console.log(field + " " + value) // this.$emit('updateField', { ...this.personalInformation, [field]: value }) this.$emit('updateField', field, value) }, }
For anyone who doesn't want to chain event emitting, there is a parent object on the child object which can also be used to emit events. Be sure to register the launch in the parent to avoid warnings in the console.
Subitems
Call the direct parent's
$emit
here.Child.vue
Or how to use:
Father
Since it is the parent that signals the ancestor, declare the emission here. For
, just use the
defineEmits()
method to declare emits. SeeDocumentation.Parent.vue
If using
grandparents
Then listen to the event in the grandparent component.
GrandParent.vue
This is how I do it:codesandbox
Emits accepts only two parameters, the emitted name and the emitted value. If multiple values are emitted, they must be emitted as a single object. In my solution, the grandchild component emits the field name and value as a single object
grandson
The child object captures and re-eits,butfirst takes care to emit in the format expected by the parent component (it requires the entire
data.primary
object because that's what is set to the v model )child
Then the parent component will automatically receive and update the v-model
data.primary
object.Or, I must mention that you can always usePinia, the official state management library for Vue (to save some state in one component and read the same from any other component status). There's certainly a learning curve, but it's definitely worth learning and is designed to simplify this type of situation.