I got a console error when I tried to list custom event in the component’s emits option like this:
PARENT
JavaScript
x
6
1
<Btn
2
event-name="toggleSideMenu"
3
@toggle-side-menu="toggleHandler">
4
toggle
5
</Btn>
6
CHILD
JavaScript
1
21
21
1
<template>
2
<button @click="handleClick">
3
<slot></slot>
4
</button>
5
</template>
6
7
export default {
8
props: {
9
eventName: {
10
type: String,
11
default: ''
12
}
13
},
14
emits: [this.eventName], // Uncaught TypeError: Cannot read property 'eventName' of undefined
15
methods: {
16
handleClick() {
17
this.$emit(this.eventName)
18
}
19
}
20
}
21
How is the correct way to get this thing to work?
Advertisement
Answer
I don’t think you’ll be able to do that.
You may need to find a way to get around this. You can still emit an event without defining it in the emits
array, but you end up losing some of the benefits.
There is an RFC/proposal for doing just that, but doesn’t look like it’s going anywhere.