P粉1558329412023-09-02 12:37:55
If you really need to do this with slots, then you have to do this Using Vue rendering functions and JSX
<script setup> import { useSlots, onMounted, onUpdated, ref, h} from 'vue'; const slots = useSlots() const index = ref(0) const current = ref(null) onMounted(() => { const defaultSlotElements = slots.default() current.value = defaultSlotElements[0] }), onUpdated(() =>{ console.log(defaultSlotElements[index]) } ) const render = () => { return h('div', { class: 'carousel'}, [ h('p', `My default slot has ${slots.default().length} elements.`), h('div', slots.default()[index.value]), h('p', `Picture ${ index.value + 1 }`), h('button', { onClick: () => { index.value = index.value + 1 == slots.default().length ? 0 : index.value + 1 } }, 'Next') ]); }; </script> <template> <render /> </template>
This is workCSRC Playground
renew
Rendering functions can be used with custom components.
Here is an attempt to build your
I don't see any other way to use the default slot
instead of using the render
function to build what you want.