Skip to content

How can I use template in preview slot? #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ALiangLiang opened this issue Feb 15, 2023 · 3 comments
Closed

How can I use template in preview slot? #17

ALiangLiang opened this issue Feb 15, 2023 · 3 comments
Labels
question Further information is requested

Comments

@ALiangLiang
Copy link

ALiangLiang commented Feb 15, 2023

How can I preview component with slot? I cannot put template in the preview slot. It resulted in the error below.

Error: Codegen node is missing for element/if/for node. Apply appropriate transforms first.

<template>
  <div>
    <slot />
  </div>
</template>

<preview lang="md">
  <script setup>
  const msgs= ['1', '2']
  </script>
  <template v-for="msg in msgs">
    <slot>
      <template>
        test {{ msg }}
      </template>
    </slot>
  </template>
</preview>
@Take-Me1010
Copy link
Contributor

Although the error is not raised in my case, the <slot /> does not seem to work.
This is because the content will be ignored if the component for <slot> is given, maybe :)
However, I found a way to preview the component in such a situation.
Importing the component itself in the <script setup> section can resolve the issue as follows:

<template>
    <div>
        <slot></slot>
    </div>
</template>

<preview lang="md">
  <script setup>
    import TestPreview from './TestPreview.vue'
    const msgs= ['1', '2']
  </script>

  <template v-for="msg in msgs">
    <TestPreview>
        test {{ msg }}
    </TestPreview>
  </template>

</preview>

Note TestPreview.vue is the file name you consider.

@ALiangLiang
Copy link
Author

Although the error is not raised in my case, the <slot /> does not seem to work. This is because the content will be ignored if the component for <slot> is given, maybe :) However, I found a way to preview the component in such a situation. Importing the component itself in the <script setup> section can resolve the issue as follows:

<template>
    <div>
        <slot></slot>
    </div>
</template>

<preview lang="md">
  <script setup>
    import TestPreview from './TestPreview.vue'
    const msgs= ['1', '2']
  </script>

  <template v-for="msg in msgs">
    <TestPreview>
        test {{ msg }}
    </TestPreview>
  </template>

</preview>

Note TestPreview.vue is the file name you consider.

Thanks. It works. slot cannot use v-model and inside <template>. So importing self component is the best solution. Hopefully documentation should mention it so developer can known about this solution.

Take-Me1010 added a commit to Take-Me1010/vite-plugin-vue-component-preview that referenced this issue Feb 19, 2023
@johnsoncodehk
Copy link
Owner

<template>
  <div>
    <slot />
  </div>
</template>

<preview lang="md">
  <script setup>
  const msgs= ['1', '2']
  </script>
  <template v-for="msg in msgs">
    <slot>
      <template>
        test {{ msg }}
      </template>
    </slot>
  </template>
</preview>

test {{ msg }} in this code is actually fallback content, so it will not be passed on.

https://vuejs.org/guide/components/slots.html#fallback-content

@johnsoncodehk johnsoncodehk added the question Further information is requested label Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants