javascript - PointerEvent object being returned instead of child data on emit - Stack Overflow

I am working on creating a vue ponent library.I have build a button ponent that has data of it's

I am working on creating a vue ponent library. I have build a button ponent that has data of it's width and left position. I'm trying to emit that data to the parent (a tabs ponent) when it's clicked. I have troubleshooted quite a bit, and have narrowed down most of the problem. My child ponent (button) is emitting the correct thing, but it looks like the parent ponent (tabs) is receiving the value of the click/pointerevent object instead of the data passed on the emit. I'm certain this is some issue in my parent click handle method, but can't pinpoint what exactly. I've included code snippets for the ponents and their click handler methods.

This is pared down, but essentially, I want to emit the width (and eventually left position) of the child button to the parent tab upon clicking the child/button. I want to assign that emitted width/left position to the slider to move some reactive underlining whenever a button is clicked in the tabs. I built in a console log statement on the click event that returns the emitted value from the child, and then returns the received value from the parent. Right now, the child is emitting the correct value when button is clicked, but parent is receiving and trying to assign a PointerEvent object. Thanks for any feedback!

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 ponent`)
      }
    }
  }
</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',
  ponents: {
   Button
  },
  methods: {
    updateSliderWidth (value) {
      this.sliderWidth = value
      console.log(`${value} has been received and assigned by parent`)
    }
  },
  data () {
    return {
      sliderWidth: ''
    }
  }
}
</script>

I am working on creating a vue ponent library. I have build a button ponent that has data of it's width and left position. I'm trying to emit that data to the parent (a tabs ponent) when it's clicked. I have troubleshooted quite a bit, and have narrowed down most of the problem. My child ponent (button) is emitting the correct thing, but it looks like the parent ponent (tabs) is receiving the value of the click/pointerevent object instead of the data passed on the emit. I'm certain this is some issue in my parent click handle method, but can't pinpoint what exactly. I've included code snippets for the ponents and their click handler methods.

This is pared down, but essentially, I want to emit the width (and eventually left position) of the child button to the parent tab upon clicking the child/button. I want to assign that emitted width/left position to the slider to move some reactive underlining whenever a button is clicked in the tabs. I built in a console log statement on the click event that returns the emitted value from the child, and then returns the received value from the parent. Right now, the child is emitting the correct value when button is clicked, but parent is receiving and trying to assign a PointerEvent object. Thanks for any feedback!

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 ponent`)
      }
    }
  }
</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',
  ponents: {
   Button
  },
  methods: {
    updateSliderWidth (value) {
      this.sliderWidth = value
      console.log(`${value} has been received and assigned by parent`)
    }
  },
  data () {
    return {
      sliderWidth: ''
    }
  }
}
</script>
Share Improve this question asked Sep 10, 2021 at 17:04 Chelsey MachinChelsey Machin 112 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I can't see any problems with your code, except that you don't use the Button ponent in the parent ponent. Instead you are using a div. This would explain, why you're getting a PointerEvent. This Event is passed as first parameter to the event, if you don't pass anything explicitly.

Here a demo: https://stackblitz./edit/vue-opruyd?file=src%2FApp.vue

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745337512a4623174.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信