ChatGPT解决这个技术问题 Extra ChatGPT

Only show slot if it has content

Is there a way to only display a slot if it has any content?

For example, I'm building a simple Card.vue component, and I only want the footer displayed if the footer slot has content:

Template

<template>
    <div class="panel" :class="panelType">
        <div class="panel-heading">
            <h3 class="panel-title">
                <slot name="title">
                    Default Title
                </slot>
            </h3>
        </div>

        <div class="panel-body">
            <slot name="body"></slot>
            
            <p class="category">
                <slot name="category"></slot>
            </p>
        </div>

        <div class="panel-footer" v-if="hasFooterSlot">
            <slot name="footer"></slot>
        </div>
    </div>
</template>

Script

<script>
    export default {
        props: {
            active: true,
            type: {
                type: String,
                default: 'default',
            },
        },

        computed: {
            panelType() {
                return `panel-${this.type}`;
            },

            hasFooterSlot() {
                return this.$slots['footer']
            }
        }
    }
</script>

In in View:

<card type="success"></card>

Since the above component doesn't contain a footer, it should not be rendered, but it is.

I've tried using this.$slots['footer'], but this returns undefined.

Does anyone have any tips?


r
radbyx

It should be available at

this.$slots.footer

So, this should work.

hasFooterSlot() {
  return !!this.$slots.footer;
}

Example.


Works! Sorry that was my fault, was using incorrect syntax when entering content into the slot. Thanks!! :)
this.$slots.footer and this.$slots['footer'] are equivalent, aren't they? Was this really the solution or was there something else going wrong?
@DanielBeck You're correct, they will both point to the same thing. And both will return undefined if there is no content. The only thing that is different is I return a boolean value, but, testing it, returning undefined from hasFooterSlot will result in v-if hiding the component.. So, technically I think your code should have worked.
This doesn't seem to work in all cases in Vue 2.6 - was incorrectly returning false when other $scopedSlots were present (at least in certain circumstances, such as an element that gets removed from the dom and added back via v-if)
Note that in Vue 3 properties in $slots are now methods, so you would need to use syntax like this: this.$slots.footer()
M
Madmadi

You should check vm.$slots and also vm.$scopedSlots for it.

hasSlot (name = 'default') {
   return !!this.$slots[ name ] || !!this.$scopedSlots[ name ];
}

But this.$scopedSlots[ name ] returns a render function, not an array of vNodes like vm.$slots do.
so then finally it coerce to boolean true.
My this.$slots was an empty object, this.$scopedSlots helps me.
See stackoverflow.com/a/60977785/752916 for an answer that takes into account that $scopeSlots[name] is a function (Vue 2.1.0+)
b
biquillo

CSS simplifies this a lot. Just use the following code and voila!

.panel-footer:empty {
  display: none;
}

This is really useful, didn't realize this existed in CSS. Thanks!
M
Mustafa Akçakaya

This is the solution for Vue 3 composition API:

<template>
    <div class="md:grid md:grid-cols-5 md:gap-6">

        <!-- Here, you hide the wrapper if there is no used slot or empty --> 
        <div class="md:col-span-2" v-if="hasTitle">
            <slot name="title"></slot>
        </div>

        <div class="mt-5 md:mt-0" 
            :class="{'md:col-span-3': hasTitle, 'md:col-span-5': !hasTitle}">
            <div class="bg-white rounded-md shadow">
                <div class="py-7">
                    <slot></slot>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {ref} from "vue";

export default {
    setup(props, {slots}) {
        const hasTitle = ref(false)

        // Check if the slot exists by name and has content.
        // It returns an empty array if it's empty.
        if (slots.title && slots.title().length) {
            hasTitle.value = true
        }

        return {
            hasTitle
        }
    }
}
</script>

S
Syed

In short do this in inline:

<template lang="pug">
  div
    h2(v-if="$slots.title")
      slot(name="title")
    h3(v-if="$slots['sub-title']")
      slot(name="sub-title")
</template>

The best answer. Suitable for Vue 3! Thanks a lot!
F
Francis Leigh

I have ran into a similiar issue but across a wide code base and when creating atomic design structured components it can be tiring writing hasSlot() methods all the time and when it comes to TDD - its one more method to test... Saying that, you can always put the raw logic in a v-if but i have found that the template end up cluttered and harder to read on occasions especially for a new dev checking out the code structure.

I was tasked to find out a way of removing parent divs of slots when the slot isnt provided.

Issue:

//instantiation show me //renders

show me

as you can see, the issue is that i have an almost 'trailing' div, that could provide styling issues when the component author decides there is no need for a bar slot.

ofcourse we could go <div v-if="$slots.bar">...</div> or <div v-if="hasBar()">...</div> etc but like i said - that can get tiresome and eventually end up harder to read.

Solution

My solution was to make a generic slot component that just rendered out a slot with a surrounding div...see below.

//slot component //usage within //instantiation show me //renders

show me

I came into use-case issues when trying this idea and sometimes it was my markup structure that needed to change for the benefit of this approach. This approach reduces the need for small slot checks within each component template. i suppose you could see the component as a <conditional-div /> component...

It is also worth noting that applying attributes to the slot-component instantiation (<slot-component class="myClass" data-random="randomshjhsa" />) is fine as the attributes trickle into the containing div of the slot-component template.

Hope this helps.

UPDATE I wrote a plugin for this so the need for importing the custom-slot component in each consumer component is not needed anymore and you will only have to write Vue.use(SlotPlugin) in your main.js instantiation. (see below)

const SLOT_COMPONENT = { name: 'custom-slot', template: `

` } const SLOT_PLUGIN = { install (Vue) { Vue.component(SLOT_COMPONENT.name, SLOT_COMPONENT) } } export default SLOT_PLUGIN //main.js import SlotPlugin from 'path/to/plugin' Vue.use(SlotPlugin) //...rest of code


this is pretty cool but has a slippery slope because of the various edge cases, scopedSlots, default slot inner content, named slots, etc.; in my humble opinion wrapping slots seems like an anti-pattern and it's probably just better to ask if the slot you want to wrap exists in the component (this.$slots). Curious how your solution has evolved since.
A
AlexMA

Initially I thought https://stackoverflow.com/a/50096300/752916 was working, but I had to expand on it a bit since $scopeSlots returns a function which is always truthy regardless of its return value. This is my solution, though I've come to the conclusion that the real answer to this question is "doing this is an antipattern and you should avoid it if possible". E.g. just make a separate footer component that could be slotted in.

Hacky solution

   hasFooterSlot() {
        const ss = this.$scopedSlots;
        const footerNodes = ss && ss.footer && ss.footer();
        return footerNodes && footerNodes.length;
   }

Best Practice (helper component for footer)

const panelComponent = { template: `

` } const footerComponent = { template: ` ` } var app = new Vue({ el: '#app', components: { panelComponent, footerComponent }, data() { return { name: 'Vue' } } }) .nice-panel { max-width: 200px; border: 1px solid lightgray; } .nice-panel-content { padding: 30px; } .nice-panel-footer { background-color: lightgray; padding: 5px 30px; text-align: center; }

Panel with footer

lorem ipsum

Panel without footer

lorem ipsum


g
grindking

Hope I understand this right. Why not using a <template> tag, which is not rendered, if the slot is empty.

<slot name="foo"></slot>

Use it like this:

<template slot="foo">
   ...
</template>

The issue is when you want external elements to wrap the slot, but don't want the external elements to be rendered when the slot isn't given or is empty. This wouldn't resolve the use case issue.
N
Non404

TESTED

So this work for me in vue 3:

I use onMounted to first get the value, and then onUpdate so the value can update.

   <template>
     <div v-if="content" class="w-1/2">
          <slot name="content"></slot>
     </div>
   </template>


 <script>
     import { ref, onMounted, defineComponent, onUpdated } from "vue";   
     export default defineComponent({
        setup(props, { slots }) {
        const content = ref()
        onMounted(() => {
                if (slots.content && slots.content().length) {
                    content.value = true
                }
            })
            onUpdated(() => {
                content.value = slots.content().length
                console.log('CHECK VALUE', content.value)
            })
})
</script>

y
yellowsir

@Bert answer does not seem to work for dynamic templates like <template v-slot:foo="{data}"> ... </template>. i ended up using:

 return (
        Boolean(this.$slots.foo) ||
        Boolean(typeof this.$scopedSlots.foo == 'function')
      );

S
Sunghyun Cho

Now, in Vue3 composition API , you can use useSlots.

<script setup>
const slots = useSlots()
</script>

<template>
  <div v-if="slots.content" class="classname">
    <slot name="content"></slot>
  </div>
</template>

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now