admin管理员组文章数量:1356304
I have a component Foo
with the following contents:
<Bar>
<template #option="slotProps" v-if="$slots.option">
<slot name="option" v-bind="slotProps"></slot>
</template>
</Bar>
<div>
<slot :selected="selectedOption"></slot>
</div>
Is there a way to pass the contents of
<slot name="option" v-bind="slotProps"></slot>
to
<slot :selected="selectedOption"></slot>
by default, while still having the ability to override it if needed?
I tried this but it didn't work
<slot :selected="selectedOption">
<template v-if="$slots.option">
<slot name="option" :selected="selectedOption"></slot>
</template>
</slot>
Edit
Got it to work with a minor change:
<slot :selected="selectedOption">
<template v-if="$slots.option">
<slot name="option" :option="selectedOption"/>
</template>
</slot>
I have a component Foo
with the following contents:
<Bar>
<template #option="slotProps" v-if="$slots.option">
<slot name="option" v-bind="slotProps"></slot>
</template>
</Bar>
<div>
<slot :selected="selectedOption"></slot>
</div>
Is there a way to pass the contents of
<slot name="option" v-bind="slotProps"></slot>
to
<slot :selected="selectedOption"></slot>
by default, while still having the ability to override it if needed?
I tried this but it didn't work
<slot :selected="selectedOption">
<template v-if="$slots.option">
<slot name="option" :selected="selectedOption"></slot>
</template>
</slot>
Edit
Got it to work with a minor change:
<slot :selected="selectedOption">
<template v-if="$slots.option">
<slot name="option" :option="selectedOption"/>
</template>
</slot>
Share
Improve this question
edited Mar 28 at 5:37
armandino
asked Mar 28 at 4:39
armandinoarmandino
18.6k17 gold badges74 silver badges85 bronze badges
2
|
1 Answer
Reset to default 0By using Vue's scoped slots and checking if the option
slot is provided, you can achieve this. If the option
slot exists, you can render it inside the default slot while allowing an override.
<template>
<Bar>
<template #option="slotProps" v-if="$slots.option">
<slot name="option" v-bind="slotProps"></slot>
</template>
</Bar>
<div>
<slot :selected="selectedOption">
<!-- Default to the 'option' slot if provided -->
<template v-if="$slots.option">
<slot name="option" :selected="selectedOption"></slot>
</template>
</slot>
</div>
</template>
本文标签: javascriptVuejs 3pass contents of a component39s slot to another slot outside itStack Overflow
版权声明:本文标题:javascript - Vue.js 3 - pass contents of a component's slot to another slot outside it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744056509a2583353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
AutoComplete
input with some custom content after user makes a selection. Sometimes this content is the same as the template for the options, but sometimes it is slightly different... – armandino Commented Mar 28 at 5:39