docs icon indicating copy to clipboard operation
docs copied to clipboard

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

Open themusecatcher opened this issue 11 months ago • 2 comments

如下代码所示,目前 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>

themusecatcher avatar Jan 17 '25 09:01 themusecatcher

see https://github.com/vuejs/core/issues/4733

edison1105 avatar Jan 17 '25 13:01 edison1105

首先非常感谢回复!但是当我花了10分钟看我 #4733 之后,我依然充满疑问与挣扎,无法获得标准实现,大家都是各自发挥😭😭,那我也先用我自己的实现吧,至少目前可以说正常工作的

themusecatcher avatar Jan 17 '25 13:01 themusecatcher