Grandparent component in VueJs 3 sends events to its grandparent component
P粉251903163
P粉251903163 2023-11-03 16:03:06
0
2
640

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

template  script 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) }, }


P粉251903163
P粉251903163

reply all (2)
P粉037880905

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$emithere.

Child.vue

export default { methods: { handleInput(e) { this.$parent.$emit('custom-event', e.target.value) } } }

Father

Since it is the parent that signals the ancestor, declare the emission here. For, just use thedefineEmits()method to declare emits. SeeDocumentation.

Parent.vue

 
export default { emits: ['custom-event'] // Register the emits }

If using

grandparents

Then listen to the event in the grandparent component.

GrandParent.vue

 
    P粉842215006

    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

    onInput(field, value) { this.$emit("update-field", { field: field, value: value }); },

    The child object captures and re-eits,butfirst takes care to emit in the format expected by the parent component (it requires the entiredata.primaryobject because that's what is set to the v model )

    child

    UpdateField({ field, value }) { const newVal = this.modelValue; newVal.personalInformation[field] = value; this.$emit("update:modelValue", newVal); }

    Then the parent component will automatically receive and update the v-modeldata.primaryobject.

    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.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!