Instead of emitting child data, a PointerEvent object is being returned

I am currently developing a Vue component library and one of the components I have built is a button that contains data for its width and left position. My goal is to emit this data to the parent (a tabs component) when the button is clicked. After troubleshooting extensively, I have identified the source of most of the issues. It seems like the child component (button) is emitting the correct data, but the parent component (tabs) is receiving the click/pointerevent object instead of the emitted data. I believe the problem lies in my parent's click handle method, but I am struggling to pinpoint the exact issue. I have provided code snippets for both components and their respective click handler methods.

Although simplified, the main objective is to emit the width (and eventually the left position) of the child button to the parent tab upon clicking the button. The emitted width/left position will be used to adjust a reactive underline slider whenever a button in the tabs is clicked. I added a console log statement to track the emitted value from the child on click as well as the received value by the parent. Currently, the child is emitting the correct value when the button is clicked, but the parent is trying to assign a PointerEvent object instead of the expected data. Any feedback would be appreciated!

Child (button) template and relevant script:

<template>
  <div class="button @click="click" ref="button"> 
    <slot />
  </div>
</template>

<script>
import { ref } from 'vue'
  export default {
    name: 'Button',
    emits: [
      'click'
    ],
    data () {
      return {
        width: '',
        left: ''
      }
    },
    setup() {
      const button = ref(null)

      return {
        button
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.left = Math.ceil(this.button.getBoundingClientRect().left)
        this.width = Math.ceil(this.button.getBoundingClientRect().width)
      })
    },
    methods: {
      click () {
        this.$emit('click', this.width)
        console.log(`${this.width} has been emitted to the tabs component`)
      }
    }
  }
</script>

Parent (tab) template and relevant script:

<template>
  <div class="tabs" @click="updateSliderWidth">
    slot
  </div>
  <div class="slider" :style="sliderWidth">
</template>

<script>
import Button from './Button.vue'

export default {
  name: 'Tabs',
  components: {
   Button
  },
  methods: {
    updateSliderWidth (value) {
      this.sliderWidth = value
      console.log(`${value} has been received and assigned by parent`)
    }
  },
  data () {
    return {
      sliderWidth: ''
    }
  }
}
</script>

Answer №1

Your code appears to be problem-free, except for one thing: you are not using the Button component in the parent component, but instead opting for a div element. The reason why you're encountering a PointerEvent is likely due to this difference. When no parameter is explicitly passed, this event is automatically sent as the first parameter.

For a demonstration, check out this link: https://stackblitz.com/edit/vue-opruyd?file=src%2FApp.vue

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

Cutting an in-memory Base64 PNG using ONLY JavaScript on the client side without relying on canvas

Scenario: Working with JavaScript in a SDK environment, either on node.js or a browser. Situation: I have a base64 string containing a PNG image encoded using base64 (extracted from selenium webdriver's takeScreenshot function). Inquiry: How can I c ...

What could possibly be causing the notification to fail to function in my deferred scenario?

I'm currently delving into the world of jquery deferred and making good progress, but I have encountered a hurdle when it comes to the notify feature. In my code snippet, you will notice that I attempted to use the notify method, only to find out that ...

Retrieve recently appended DOM elements following the invocation of createComponent on a ViewContainerRef

I have a current function in my code that dynamically creates components and then generates a table of contents once the components are added to the DOM. This service retrieves all h3 elements from the DOM to include in the table of contents: generateDy ...

Comparing tick and flushMicrotasks in Angular fakeAsync testing block

From what I gathered by reading the Angular testing documentation, using the tick() function flushes both macro tasks and micro-task queues within the fakeAsync block. This leads me to believe that calling tick() is equivalent to making additional calls pl ...

When extracting information from a table within a Vue.js and Vuetify.js function

When trying to display a table, only one row is being printed. How can I resolve this issue? I have attempted various solutions (Vanilla JS worked). This project is for educational purposes and I am relatively new to JS and Vue.js. <template> < ...

What is the best way to resize a mesh in three.js?

I'm facing a challenge in scaling down a mesh within three.js, despite trying the code below: Could really use some guidance on this! alterMesh(vAngles, hAngles, intensities) { let vLines = this.getVerticalDistribution(vAngles, hAngles, intensi ...

Application route is failing to direct to the HTML page

On my MEAN stack website, I am trying to make the browser navigate to a file named 'findCable.html' but it keeps directing to 'singleCable.html'. I have double-checked the angular routing file and backend routes, but can't see ...

Combine information entered into fields and insert either a "?" or "!" to generate a hyperlink with Vue.js

Creating a link through multiple input fields is essential for my project, resulting in a structure reminiscent of http://example.com?name=xxx&lastname=xxx... The initial name and last name are obtained from the respective input fields. Beneath the f ...

Integrating HTML, JavaScript, PHP, and MySQL to enhance website functionality

Exploring the intricacies of HTML, JavaScript, PHP, and MySQL, I have been working on an order form to understand their interactions. View Order Form The aim of this table is to allow users to input a quantity for a product and have JavaScript automatica ...

Show Angular if it is in the array

Presently, I have an ng-repeat function to display comments on articles. It is structured like this: <div ng-repeat="comment in comments"> <li class="item" ng-class-even="'even'"> <div class="row"> ...

I'm encountering a npm error on Windows_NT 10.0.19042, does anyone know how to troubleshoot this issue?

After downgrading to [email protected], I encountered an error message that keeps popping up whenever I try to update npm or install new packages. What steps can I take to resolve this issue? npm ERR! Windows_NT 10.0.19042 npm ERR! argv "C:\ ...

Creating a repository of essential functions in AngularJSDiscover the steps to set up a

I am looking to create a set of reusable functions in AngularJS for CRUD operations that can be used across multiple entities in my project. I have already set up a factory using $resource for server communication, which looks like this: Model File: var ...

Having trouble with saving data when clicking in a react.js app? Check out the codepen link provided for a

I'm currently diving into the world of react.js and tackling a small project. In this project, I'm exploring how to incorporate tagging functionality. The tags will essentially be static text associated with each transaction. My challenge lies in ...

Prevent elements from displaying until Masonry has been properly set up

My goal is to merge Masonry elements with existing ones. Currently, the items appear before Masonry initializes then quickly adjust into position a moment later. I want them to remain hidden until they are in their proper place. This is the snippet (with ...

"Troubleshooting: Issue with AngularJS ng-repeat not functioning properly when using an

I am working with a simple list <ul> <li ng-repeat="spiel in spielListe">Do something</li> </ul> Along with a perfectly connected controller $scope.spielListe = []; There is also a method that adds objects to the array in th ...

How can I get video playback in IOS to work with Videogular2 using HLS?

I recently integrated videogular2 into my Angular 6 app to display HLS streams. Everything seems to be working smoothly on desktop and Android devices, but I encountered an error when testing on IOS: TypeError: undefined is not an object (evaluating &apos ...

PHP trigger update query to modify table data on click event

I specialize in creating system notifications and have encountered an issue. My goal is to update the 'notifications' table from 'new=1' to 'new=0' for a logged-in user after a click event. How can I achieve this database up ...

Generate text input fields dynamically and store their values in an array using the Backbone.js framework

Is there a way to dynamically create text boxes based on a number input field with type='number'? Essentially, every time a user increments the number input, a new text box should be added to the backbone.js view. Additionally, when values are en ...

What is the mechanism by which Redux sends state to mapStateToProps?

I'm currently delving into the inner workings of React and Redux, and recently came across FuelSavingsPage.js in this sample project. While I grasp how actions is obtained in mapDispatchToProps, I am uncertain about how state is passed to mapStateToPr ...

Having trouble with ReactJS updating the state in the correct format using moment?

In my ReactJS project, I am using a datepicker feature. However, I have encountered an issue where the state is not being updated with the selected date value after formatting it using moment.js. const [selectedDates, setSelectedDates] = useState([]) ...