Skip to content

Compiler should warn about duplicate/conditional slots #5700

@jaa134

Description

@jaa134

Version

3.2.31

Reproduction link

vue-next-template-explorer.netlify.app

Steps to reproduce

Look at the AST for the SFC. In the dev console, you can see the component has only one child.
image

The workaround example (from below) has the part that is missing. branches is present for conditional rendering
image

What is expected?

Both named-slots are registered in the AST.

What is actually happening?

Only one of the conditional templates with named slots is registered in the AST. The second is not.


Reference: vuejs/language-tools#1150

Workaround: not put a condition on template of named slot but instead make the content conditional

SFC that generates bad AST

<script
  setup
  lang="ts"
>
  import { ref } from 'vue';
  import ComponentWithSlots from '@/src/foobar';

  const aVariableThatExists = ref(true);
</script>

<template>
  <ComponentWithSlots>
    <template
      #content
      v-if="aVariableThatExists"
    >
      <div>{{ aVariableThatExists }}</div>
    </template>
    <template
      #content
      v-else
    >
      <div>{{ thisVariableDoesNotExist }}</div>
      <div>{{ thisShouldBeUnderlinedInRed }}</div>
    </template>
  </ComponentWithSlots>
</template>

Workaround SFC

Move the conditional logic into the children of a single slotted template. Define an extra set of template tags that the conditional logic can live on:

<script
  setup
  lang="ts"
>
  import { ref } from 'vue';
  import ComponentWithSlots from '@/src/foobar';

  const aVariableThatExists = ref(true);
</script>

<template>
  <ComponentWithSlots>
    <template
      #content
    >
      <template v-if="aVariableThatExists">
        <div>{{ aVariableThatExists }}</div>
      </template>
      <template v-else>
        <div>{{ thisVariableDoesNotExist }}</div>
      </template>
    </template>
  </ComponentWithSlots>
</template>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions