Skip to content

关于如何判断插槽是否有内容渲染的疑问 #3149

Open
@themusecatcher

Description

@themusecatcher

如下代码所示,目前 vue 似乎没有提供在组件中用于判断插槽是否存在的方法,而在我自己实现组件库时,发现判断插槽是否存在是非常普遍使用的一个功能,而 vue 只提供了一个 useSlots() 方法,但是实际中有很多不同的情况需要处理,目前我也无法保证方法一定没有问题,只能是遇到一种情况就处理一种情况。无奈最终自己写了一个判断插槽是否存在的方法:https://themusecatcher.github.io/vue-amazing-ui/utils/functions/slots-exist.html
希望能尽快推出一个判断插槽是否存在的 api

子组件 Child.vue:

<div>
  <slot name="title">{{ title }}</slot>
</div>

父组件 Parent.vue:

<Child>
  <template #title></template>
</Child>

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions