Annotations have an impact on the length of the slot
Vue version
3.3.8
Link to minimal reproduction
Steps to reproduce
run
What is expected?
The value of Comp2 should be 1
What is actually happening?
The value of Comp2 is 2
System Info
No response
Any additional comments?
No response
This problem does not occur in production mode, because Vue compiler will delete comment nodes in production mode.
Comment nodes are allowed as valid child elements. Although they don't affect the rendering of the component's default slot, they might lead to confusion when users check for the presence of the default slot using the useSlots
API (related issue #7699).
The issue with inconsistent results in slot.default().length
in the provided minimal reproduction is caused by different handling of comment nodes when passed into the default slot of a component in packages/compiler-core/src/transforms/vSlot.ts
. This behavior depends on whether <template v-slot>
is present or not (in L325, L342):
Deciding on changes to how comment nodes are handled in slots, requires further discussion and work. If you need to check whether there is actual content rendered in the default slot using useSlots
, you can use the following workarounds:
- Remove comment nodes to avoid interference.
- Filter out comment nodes when checking, as below:
<script setup>
import { useSlots, Comment } from 'vue';
const foo = useSlots()
.default()
.filter((i) => i.type !== Comment);
</script>
Hope this helps.
Comment nodes are allowed as valid child elements. Although they don't affect the rendering of the component's default slot, they might lead to confusion when users check for the presence of the default slot using the
useSlots
API (related issue #7699).The issue with inconsistent results in
slot.default().length
in the provided minimal reproduction is caused by different handling of comment nodes when passed into the default slot of a component inpackages/compiler-core/src/transforms/vSlot.ts
. This behavior depends on whether<template v-slot>
is present or not (in L325, L342):Deciding on changes to how comment nodes are handled in slots, requires further discussion and work. If you need to check whether there is actual content rendered in the default slot using
useSlots
, you can use the following workarounds:
- Remove comment nodes to avoid interference.
- Filter out comment nodes when checking, as below:
<script setup> import { useSlots, Comment } from 'vue'; const hasDefaultSlot = useSlots() .default() .filter((i) => i.type !== Comment); </script>
Hope this helps.
Weird, not to be exaggerated things, but I need to filter them out