The Vue object has a really helpful member called $attrs
. What $attrs does is contain all the attributes that aren't recognized as props for the current component. A good example of $attrs
is here.
I am wondering if there is an equivalent for $attrs
for $scopedSlots
. I am currently using an approach similar to the first suggestion from https://stackoverflow.com/a/50892881/6100005 . The issue with $scopedSlots
is it also passes already-defined slots. To use that example here:
<template>
<wrapper>
<b-table :foo="foo" v-bind="$attrs" v-on="$listeners">
<template v-for="(_, slot) of $scopedSlots" v-slot:[slot]="scope"><slot :name="slot" v-bind="scope"/></template>
</b-table>
<slot name="mySlot"></slot>
</wrapper>
</template>
<script>
export default {
props: {
// let's pretend that `b-table` has a prop `foo` that is default `false`
foo: {
type: boolean,
default: true,
}
}
}
</script>
Now, foo
will not get binded twice, thanks to the behavior of $attrs
, but mySlot
will be sent to both wrapper
and b-table
.
So how can pass I down all the slots except for the one I'm defining myself?
One idea I have is to have
computed: {
bTableSlots() {
Object.keys(this.$scopedSlots)
.filter( key => key!=='mySlot' )
.reduce( (res, key) => (res[key] = this.$scopedSlots[key], res), {} );
}
}
And then
<template v-for="(_, slot) of bTableSlots" v-slot:[slot]="scope"><slot :name="slot" v-bind="scope"/></template>
Was wondering if there were a better way to do this. Thanks!