So I have a button that I want to use multiple times, as a component with a slot
<div name="checkAnswer" class="w-[70%] mx-[15%] flex items-center justify-center" > <button class="p-3 rounded-3xl shadow-md font-bold m-4 px-10 border-2 border-grey-800 hover:border-black hover:transition-all hover:duration-500" > <slot name="checkAnswer"></slot> </button> </div>
But when I wanted to use it, I couldn't use @click="method" on the slot button, so I used a nested button (I had a slot button and then another button just to Use @click="method"):
<template #checkAnswer> <button @click="checkAnswer" :disabled="isAnswerChecked" :class="{ ' text-gray-300 border-gray-300 ': isAnswerChecked, }" > Check answer </button> </template>
This works, but it's invalid HTML. How can I solve it?
Vue3 SFC Playground
You need to use
v-bind="$attrs"
to bind the properties of the button component to<button>
in the template, and disable the For default attribute inheritance, useinheritAttrs:false
.Also, you don't need to use named slots here, just use the default ones:
Parent component: