Fragment component #448
Replies: 9 comments 12 replies
-
How is // Code slightly simplified for clarity
function wrapper({ shouldWrap }, { slots }) {
return shouldWrap
? h("Wrapper", slots.default())
: slots.default();
} |
Beta Was this translation helpful? Give feedback.
-
Strange why |
Beta Was this translation helpful? Give feedback.
-
Fragment.vue 🤣 <template>
<slot />
</template> |
Beta Was this translation helpful? Give feedback.
-
If something like this does eventually get implemented, I think it would make a lot more sense to use a directive, analogous to the
Conditionally rendering wrapper elements/components is such a basic operation, the syntax should match that in terms of simplicity. |
Beta Was this translation helpful? Give feedback.
-
Is there any news on this? The conditional wrapper feature would be really useful and I'm wondering whether something was already implemented in this regard? Either as a custom directive or via the |
Beta Was this translation helpful? Give feedback.
-
Any new status update? :) |
Beta Was this translation helpful? Give feedback.
-
What I did as a patch for this missing feature was to create a helper component / module in my utils directory (under
import { defineComponent } from 'vue'
export const useFragment = defineComponent({
inheritAttrs: false,
render() {
return this.$slots && (typeof this.$slots.default === 'function' ? this.$slots.default() : '')
},
}) In the template I used it like this: <template>
<Component :is="!isDocument ? 'a' : useFragment" :href="item.url" target="_blank">
{{ item.title }}
</Component>
</template>
<script setup lang="ts">
import { useFragment } from '@/utils'
[...]
</script> I hope this helps someone looking for a solution to this. It would still be better if it was integrated as a default functionality in Vue. I would highly appreciate it if someone from the core team could attend to this matter. |
Beta Was this translation helpful? Give feedback.
-
Inspired by @distor-sil3nt, I made a Nuxt 2 (Vue 2.7) compatible component for optional wrapping: <script>
export default {
name: 'Fragment',
render(h) {
if (this.$slots && this.$slots.default) {
return this.$slots.default;
}
return null;
},
};
</script> usage: <component :is="isLink ? 'a' : 'Fragment' :href="url">Click me</component> |
Beta Was this translation helpful? Give feedback.
-
Looks like a neat solution in Vue 3 |
Beta Was this translation helpful? Give feedback.
-
This is a discussion for Fragment component proposal.
Beta Was this translation helpful? Give feedback.
All reactions