Search code examples
javascriptvue.jsvue-multiselect

Same slot content for multiple template slots


In vuejs, is there a way to set the same content for multiple slots without copy pasting?

So this:

<base-layout>
  <template slot="option">
    <span :class="'flag-icon-'   props.option.toLowerCase()" />{{ countriesByCode[props.option] }}
  </template>

  <template slot="singleLabel">
    <span :class="'flag-icon-'   props.option.toLowerCase()" />{{ countriesByCode[props.option] }}
  </template>
</base-layout>

Could be written that way:

<base-layout>
  <template slot="['option', 'singleLabel']">
    <span :class="'flag-icon-'   props.option.toLowerCase()" />{{ countriesByCode[props.option] }}
  </template>
</base-layout>

Thanks a lot.


Solution

  • You could try using v-for for that.

    <base-layout>
      <template :slot="slotName" v-for="slotName in ['option', 'singleLabel']">
        <span :class="'flag-icon-'   props.option.toLowerCase()" />{{ countriesByCode[props.option] }}
      </template>
    </base-layout>

    See working example.