Question about useSlots() - not reactive? #6096
-
i need to access the slots content in script setup to count the children of the default slot.
And within template just But i recognize a difference in both. In template $slots seems to be reactive when slot content changes (example change from 4 to 3 children in slot). The value updates. But useSlots seems not to be reactive. I don't get it running to get the update when slot content gets added or removed. i tried to use a ref, i tried to use a computed, it looks like i can only access the initial value on component setup but no further updates when slot content changes. Is this difference expected? How would i need to handle it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You can use like this Example, Hope this helps you. new.mp4 |
Beta Was this translation helpful? Give feedback.
-
@liulinboyi Should that still be possible with latest vue / nuxt version? |
Beta Was this translation helpful? Give feedback.
-
I know this doesn't directly answer the OP's question, but in case anyone needs to make it reactive based on the current viewport, this approach worked for me: import { ref, watch } from 'vue';
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core';
const slots = defineSlots<{
footer(): VNode[];
}>();
const breakpoints = useBreakpoints(breakpointsTailwind);
const hasFooterSlot = ref(false);
watch(
() => breakpoints.active().value,
() => {
hasFooterSlot.value = !!slots.footer?.()?.length
},
{ immediate: true }
); |
Beta Was this translation helpful? Give feedback.
You can use like this Example, Hope this helps you.
new.mp4