Interact with parent function from child component using data attribute in Vue.js

I have a parent component called Waypoint that contains child elements. I want to be able to call functions on these child elements from the parent component. How can I achieve this?

//coding example

//template
<Waypoint>
  <div 
    :data-wp-cb="handleAnimation"
    // :data-wp-cb="handleAnimation.bind(this)"  
    class="js-wp"

//methods
handleAnimation() {
  // do something
}

//Waypoint component

update(element) {
  const callback = element.dataset.wpCb
  callback() // callback is not a function
}

What steps can I take to resolve this issue and successfully execute the desired functions?

Answer №1

If you want to solve this, make sure to trigger the event from the child component to the parent component.

Example of a Waypoint component:

update(param) {
  this.$emit('update-from-child', param);
}

Example of a Page component:

<v-waypoint @update-from-child="localMethod">
  <div 
    :data-wp-cb="animateIn"
    // :data-wp-cb="animateIn.bind(this)"  
    class="js-wp"

This is how Vue handles communication from child components to parent components. For more information, refer to the official documentation.

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

What is the best way to trigger an event in VueJS?

I recently implemented a table using Vuetify in my project. The table is now split into two components - the Table component and the Row component. My challenge is how to handle the same function, this.selected = !this.selected!, when dealing with 2 differ ...

After being initialized, the added Vue.js DOM elements do not function together

I updated an HTML page with ajax contents and incorporated Vue.js for front-end events. Unfortunately, the dynamically added elements are not interacting with the Vue.js instance, even when I attempted to forceUpdate them. Any suggestions on how to resol ...

Nuxt - Issue persisting logged in state on refresh despite information being stored in local storage and cookies

I am facing an issue where the state gets cleared on refresh, even though the token, userid, user email, and expiration date are stored in local storage and cookies. I suspect there might be a problem with the store or something else needs to be done to re ...

Tips for creating a dynamic component with v-for and utilizing dynamic slots

I am facing an issue with a child component that utilizes v-for. My goal is to allow the parent component to dictate how each item within the v-for loop is displayed by passing down a slot or similar mechanism. The challenge lies in the fact that the paren ...

What could be causing my Vuetify Stepper Component to be hidden?

I am utilizing Vue and Axios to display data using the Stepper Component from Vuetify. However, after adding the API data, it seems that the CSS class is somehow being set to display: none;. I am confused as to why this is happening, so any help would be g ...

Problem with JWT authentication causing SockJS handshake to block WebSocket connection attempts

I have an operational Spring Boot server with Authentication/Authorization features. However, I am facing issues when trying to establish a connection with SockJS due to my security protocols blocking it. Although I do not have a complete understanding of ...

html table displaying incorrect data while using dynamic data in vue 3

example status 1 Hello there! I'm trying to create a table (check out example status 1 for guidance). Here's the code snippet I am using: <table> <tr> <th>Product</th> <th>Price</th> <th>Av ...

I am having trouble getting Vue.js to function properly within HTML when using Django. Can someone please lend me a

The code run Here is the HTML document: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Register</title> <!--javascript extensions--> <script src=' ...

ERESOLVE encountered issues resolving the dependency tree for a project using .NET Core 3.1 and Vue.js framework

I encountered an issue while trying to build my application. The error message is as follows: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class ...

Encountering an issue while invoking the helper function in Vuejs

Main view: <script> import { testMethod1 } from "../helper"; export default { methods: { init(){ console.log("Res:", testMethod1()); } } } </script> Helper: import DataService from "../services/data. ...

Ways to trigger a Vue.js method after a delay of 500 milliseconds

Currently, I am developing a search system that triggers an ajax call with every key press. However, I would like to implement a delay of 500ms before the ajax call is made after typing begins. Any ideas on how this can be achieved? ...

Is there a way to access data dynamically during rendering and display the outcome?

Upon rendering the object below, I am able to implement something similar to: <div v-for="(card, groupIndex) in cards" v-bind:key="card.id"> <div> {{cards[groupIndex].group.length}} </div> </div> This code snippet provides a cou ...

Help with Vue.js App Deployed on Heroku: How to Fix "Cannot GET /" Error?

When I try to access the URL for my Vue.js application (), it correctly takes me to the homepage. However, clicking on the 'Start' button, which should redirect me to '/test', gives me an error message saying 'Cannot GET /test&apos ...

Showing navigation items in Vuejs based on authentication status

In my current project, I am developing a Single Page Application (SPA) using vuejs with vuetify and integrating authentication via a laravel/passport API. The challenge I'm facing is related to conditional rendering of navigation icons based on user a ...

Having trouble executing a Vue project as I encountered an error stating: "Module not found: '@vue/cli-plugin-babel'". To fix this, I proceeded to install the necessary dependencies by running the command: npm install

Error: The module '@vue/cli-plugin-babel' could not be found. Require stack: C:\Users\HP\AppData\Roaming\npm\node_modules@vue\cli-service\lib\Service.js C:\Users\HP\AppData\Roaming ...

Find all objects in an array of objects that contain at least one value that matches a given string

I am currently integrating search functionality in my application. The UI search results are generated from an array of objects. My goal is to loop through the name, custNumber, and sneak values in each object and display only the ones that contain a subst ...

The process of implementing custom error messages for each input field in VueJs Element

Within my application, there is a form that I am using for server-side validation. However, I am unsure of how to add error messages for specific inputs. The form can be found at . ...

Having trouble creating an axios instance in Vue

I recently came across a helpful tip in an article about using axios for AJAX requests in Vue. The author mentioned that by setting Vue.prototype.$http = axios, we can then use this.$http within the Vue instance, which worked perfectly for me. However, wh ...

Working with dynamic checkbox values in VueJS 2

In my project using VueJS 2 and Vuetify, I am creating a subscription form. The form is dynamic and fetches all the preferences related to a subscription from the server. In the example below, the preferences shown are specifically for a digital magazine s ...

How to avoid console messages when dealing with Axios 422 error handling?

When developing my application, I decided to use Laravel for the backend and Vue for the frontend. To handle validation errors, I chose to implement code 422 based on recommendations from this article. The PHP code snippet in my RegisterController: if ($t ...