Vue3:检查事件监听器是否绑定到组件实例

Vue3: Check if event listener is bound to component instance

我有一个可重复使用的 Badge 组件。当组件实例上存在 onDelete 事件侦听器时,我希望能够添加一个 close/delete 按钮。

<template>
    <div class="flex inline-flex items-center px-2.5 py-0.5 text-xs font-medium select-none" :class="[square ? '' : 'rounded-full']">
        <slot />
        <button class="cursor-pointer ml-2" @click="$emit('onDelete')">
            <XIcon class="flex-shrink-0 h-3.5 w-3.5 text-gray-400 hover:text-gray-500" aria-hidden="true" />
        </button>
    </div>
</template>

<script>
    import { XIcon } from '@heroicons/vue/solid';

    export default {
        props: {
            color: { type: String },
            square: { type: Boolean, default: false },
        },
        components: {
            XIcon,
        },
        emits: ['onDelete'],
    };
</script>

如果我向按钮添加 v-if 语句,则立即执行 emit 事件

<button v-if="$emit('onDelete')" class="cursor-pointer ml-2" @click="$emit('onDelete')">

我正在使用 Vue 3

更新:如果您的组件使用新的 emits option in Vue3, which is the recommended best practice from the Vue3 docs, the event listeners will not be apart of the $attrs. An issue will be submitted to the Vue team for clarification and guidance on why this behaves this way.


我在 StackBlitz 中简化了上面的示例,以隔离您需要的功能。

重要提示,我使用的是 Vue 3.2.26。

在 Vue3 中 $listeners were removed.

事件侦听器现在是 $attrs 的一部分。然而,在 Badge 中简单地控制台日志记录 this.$attrs 不会显示您要查找的键,它们在 targets 中,但可以通过在绑定事件名称前加上 on 来访问。在 Badge 组件中,您将使用 onOnDelete.

带有两个徽章的完整工作示例。一个会显示,因为它有绑定事件 onDelete,另一个不会显示,因为绑定 onDelete 不存在。

https://stackblitz.com/edit/vue-8b6exq?devtoolsheight=33&file=src/components/Badge.vue