Skip to content

How to use a Vue slot with a template #12691

Answered by tylercollier
mikestu asked this question in Help
Discussion options

You must be logged in to vote

Here's how I did it in Vue 2. I'm using this example button component with a default slot and an optional "hint" slot.

Button.vue:

<template>
  <button @click="$emit('onClick')">
    <slot>
      Submit
    </slot>
    <div style="font-size:x-small;color:gray;">
      <slot name="hint">
        Click me
      </slot>
    </div>
  </button>
</template>

<script>
export default {
}
</script>

Button.stories.js:

const Template = (args, { argTypes }) => {
  return {
    props: Object.keys(argTypes),
    components: { Button },
    template:
      `
      <Button @onClick="onClick" v-bind="$props">
        <template v-if="${'default' in args}" v-slot>${args.default}</template>
        <template…

Replies: 14 comments 26 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@vchan14
Comment options

@euaaaio
Comment options

@mryellow
Comment options

Comment options

You must be logged in to vote
16 replies
@tylercollier
Comment options

@braddialpad
Comment options

@zhixuan-loh
Comment options

@yonario
Comment options

@younho9
Comment options

Answer selected by mikestu
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@BryanAdamss
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Christo44
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@Davo911
Comment options

@sherif414
Comment options

@matthew-dean
Comment options

@JoJk0
Comment options

@JoJk0
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet