Skip to content

Instantly share code, notes, and snippets.

@megurock
Last active September 22, 2023 04:43
Show Gist options
  • Save megurock/25e7aca0e189f76cf833c9f700973396 to your computer and use it in GitHub Desktop.
Save megurock/25e7aca0e189f76cf833c9f700973396 to your computer and use it in GitHub Desktop.
slot に対する変更(script setup を使わない)
<script>
import { mergeProps } from 'vue'
export default {
setup(props, context) {
const slotContent = context.slots?.default?.()
if (slotContent) {
slotContent.forEach(vNode => {
vNode.props.class = (vNode.props.class ?? '') + ' foo'
vNode.props.style = mergeProps(vNode.props.style, style)
})
}
return () => slotContent ?? null
}
}
</script>
<template>
{{ render }}
</template>
@megurock
Copy link
Author

<script setup> の場合

<script setup>
import { mergeProps, useSlots } from 'vue'

const props = defineProps({ ratio: Object})
const slotContents = useSlots().default?.()
if (slotContents) {
  slotContents.forEach(vNode => {
    vNode.props.class = (vNode.props.class ?? '') + ' foo'
    vNode.props.style = mergeProps(vNode.props.style, style)
  })
}
</script>

<template>
  <slot />
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment