Techniques for transmitting an event from a deeply nested child component?

Can someone help me with this issue I'm facing with my components? I have a CustomForm component that has a submit event, but it's only accessible in the parent component. Is there a straightforward solution to get access to the submit event without using an event bus?

This is how my CustomForm.vue looks like:

<template>
  <form @submit.prevent="submit">
    <slot />
  </form>
</template>

<script>
export default {
  methods: {
    submit() {
      this.$emit('onSubmit')
    }
  }
}
</script>

And here is FormHolder.vue:

<template>
  <div>
    <CustomForm>
      <slot />
      <FormSubmit />
    </CustomForm>
  </div>
</template>

In Page.vue, I want to be able to access the submit event from CustomForm component:

<template>
    <FormHolder @onSubmit="submit">
      My input fields...
    </FormHolder>
</template>

Answer №1

To achieve communication without using an event bus or Vuex, it is necessary to pass the event up the chain so that when CustomForm triggers an event, FormHolder also emits an event that reaches Page:

<template>
  <div>
    <CustomForm @onSubmit="submit">
      <slot />
      <FormSubmit />
    </CustomForm>
  </div>
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to enable noWrap in Vue2Leaflet?

Is there a way to prevent infinite tile scrolling using vue2leaflet? I have tried the following code: <l-map :zoom="zoom" :center="center" :noWrap="true" :bounds="maxBounds"> maxBounds: [[-90, -180],[90, 180]] Unfortunately, this approach is not ...

Having difficulty interacting with a JavaScript button using Selenium

I've searched high and low on various platforms for a solution to my issue, but unfortunately, nothing seems to be effective. While this article seemed promising, it still didn't do the trick: How to click href javascript:void(0) and hidefocus=t ...

Tips for importing in VScode without including an index at the end of the URL

I'm attempting to set up vs-code so it can identify and propose imports without the need for an index file. foo (folder) - index (js file) current behavior: import './foo/index' expected behavior: import './foo' I've tri ...

What are the steps to troubleshoot a Node Package Manager library in Visual Studio Code?

I have created a Typescript library that I plan to use in various NodeJS projects. The source code is included in the NPM package, so when I install it in my projects, the source also gets added to the node_modules folder. Now, during debugging, I want to ...

What is the best way to display a 'confirmed' message on my registration page once a user has submitted their information?

I have set up a nodejs server and developed a registration page using HTML. When users click the submit button, the server I built receives the input data and validates it. If the user is successfully added to the database, I want to display a new message ...

Utilize JSON parsing to extract and store data into an object

I'm currently working on extracting specific objects from a parsed JSON stored within an object named data.json. var data = { json: '', text: '', welcome: '', q1: '', } let foo = await fetch(spr ...

Easily targeting elements and their descendants in jquery: What you need to know!

Within a div, there are nested divs that contain tables and other divs. How can I use jquery to select both the parent div and its children? The parent div has the class "AccordionTitle AccordionTitle-selected". I have attempted the following: var klo=$( ...

Having trouble locating an element, Sammy?

I am encountering an issue while using Sammy for my SPA. The error message I receive is: [Sun Mar 29 2020 17:37:19 GMT+0300 (Eastern European Summer Time)] #main 404 Not Found get / Error: 404 Not Found get / at Sammy.Application.error (sammy-latest ...

Tips for preventing Unknown event codes in POST request responses

When my client makes POST requests to the nodejs server I am running, I receive "unknown event 72" messages in the POST response, as shown in the Wireshark screenshot below. These extra data points are causing unnecessary bandwidth usage for my application ...

I am facing an issue where the group-hover feature does not seem to be functioning properly

My NextJS app seems to be having trouble with group-hover functionality. I've experimented with different ways of declaring group-hover at various levels, but it doesn't seem to be working properly. Interestingly, when I changed group-hover to ...

The issue with ng-if not functioning within ng-repeat is that the ng-if directive

My issue is with using ng-if inside an ng-repeat in AngularJS. Despite updating to the latest version on 09/27/2014, I am still unable to make it work properly. The code functions perfectly outside of ng-repeat, and also works fine inside ng-repeat when us ...

What is the best way to refresh or manually trigger the marker cluster icon update in Google Maps API?

Recently, I implemented a custom cluster HTML marker, where I attempted to dynamically change the color of my markers within the cluster. Despite debugging the code and confirming that the color variable was being updated correctly, the marker colors thems ...

Conceal Element in React if it is the Final Item

Is there a way to hide the TimelineConnector if it is on the last item? I am looking for some guidance on how to achieve this. Please take a look at my codesandbox below: CLICK HERE {timelines.lastIndexOf(index) !== 1 ? <TimelineConnector /> : &quo ...

Vue 3 throws an error stating: "An uncaught DOMException occurred because the string contains an invalid character."

Exploring the capabilities of vue.js on a basic website. The setup consists of a single index.html file and all JavaScript is housed in an index.js file. The website is a work in progress, but there are no blockers preventing the JavaScript functionality ...

In JavaScript, I would like to be able to input an end date and have the program calculate and display the number of days remaining until the deadline

I'm working on a school project that involves JavaScript code. I'm struggling with converting a date prompt from string to number format. As a beginner in JavaScript, I could really use some guidance. <script> enddate = prompt('Wh ...

Exploring the attributes of ExtJS display objects

I am looking for a way to efficiently display JSON content from a servlet in a browser using a list format. While I could use the definition list tag in pure HTML, I need to load everything dynamically without manually parsing and creating the HTML code. ...

Issues with Alignment of Navbar Elements in Bootstrap 3

When I test my website locally, the elements are aligned correctly as expected. However, when I view it on my live webpage, the elements are all left-aligned. I've attempted a solution from this source, but the issue persists. What confuses me is why ...

Basic example of jQuery in an ASPX file

I can't figure out why this basic example is not working. In my WebApplication, I have a script: function myAlert() { $("#Button1").click(function () { alert("Hello world!"); }); } In my asp page, I have the following code: < ...

Leveraging Vue's `:is` property for dynamically rendering components using a configuration object

I have created a confirmation popup Vue component with the following configuration object: { title: null, message: null, onConfirm: null, onDismiss: null, modal_class: null, icon: null, confirmBtnText: null, confirmBtnColor: null, compon ...

Prevent Chrome from automatically restoring the previous browsing session

Is there a way to prevent Chrome from restoring the session when reopening a closed page? Possibly using a special header? My company app relies heavily on user state, and I need to log employees out and delete all relevant data if they are inactive for 6 ...