gpt4 book ai didi

vue.js - 仅在有内容时显示插槽

转载 作者:搜寻专家 更新时间:2023-10-30 22:07:54 24 4
gpt4 key购买 nike

有没有办法只显示有内容的插槽?

例如,我正在构建一个简单的 Card.vue 组件,我只希望在页脚槽有内容时显示页脚:

模板

<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>
export default {
props: {
active: true,
type: {
type: String,
default: 'default',
},
},

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

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

在 View 中:

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

由于上面的组件不包含页脚,它不应该被渲染,但它是。

我试过使用 this.$slots['footer'],但这会返回 undefined。

有人有什么建议吗?

最佳答案

它应该在

this.$slots.footer

所以,这应该可行。

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

Example .

关于vue.js - 仅在有内容时显示插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44077277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com