如何处理 Vue3 和 Storybook 7 中的插槽
P粉504920992
P粉504920992 2023-08-26 00:23:54
[Vue.js讨论组]
<p>我正在尝试将一个组件(UIButton)作为 ButtonAsSlot 故事中另一个组件(UICard)中的插槽传递。</p> <p>在 ButtonAsSlot 故事中,我传递 <code>default: '<uibutton v-bind="{type: 'button', isDisabled: false,variant: 'primary', label: 'test ',}">',</uibutton></code> 作为 args.default,因此它将在插槽中传递。</p> <p>如果我可以传递现有的故事(例如<code>默认值:'<uibutton v-bind="ImportedStory.args">'</uibutton></code>),那就太好了。然而,这是行不通的。</p> <p>我尝试了以下操作,但出现错误<code>错误:Storybook 上出现意外的标识符“对象”</code>。</p> <pre class="brush:php;toolbar:false;">const disabledArgs = ImportedStory.args export const ButtonAsSlot: Story = { ...Playground, render: (args, { argTypes }) =&gt; ({ components: { UICard, UIButton }, props: Object.keys(argTypes), setup() { return { ...args, } }, template: ` &lt;UICard v-bind="args"&gt; &lt;template v-if="$props.default" v-slot&gt; &lt;p&gt;↓Content of slot displayed below&lt;/p&gt; ${args.default} &lt;/template&gt; &lt;/UICard&gt; `, }), args: { ...Playground.args, default: `&lt;UIButton v-bind="${disabledArgs}" /&gt;`, },</pre> <p>如何使用参数中的现有故事传递组件并将其设置在插槽中?</p> <p>我使用的是Vue3。代码如下。</p> <p>--Card.stories.ts</p> <pre class="brush:php;toolbar:false;">import type { Meta, StoryObj } from '@storybook/vue3' import UICard from './Card.vue' import UIButton from '~/components/Clickable/Button.vue' import { setDefaultProps } from '~/.storybook/utils' const meta: Meta&lt;typeof UICard&gt; = { title: 'Elements/Card', component: UICard, tags: ['autodocs'], } export default meta type Story = StoryObj&lt;typeof UICard&gt; export const Playground: Story = { story: { name: 'Default', }, render: (args, { argTypes }) =&gt; ({ components: { UICard }, props: Object.keys(argTypes), setup() { return { ...args, } }, template: ` &lt;UICard v-bind="args"&gt; &lt;template v-if="$props.default" v-slot&gt; ${args.default} &lt;/template&gt; &lt;/UICard&gt; `, }), } setDefaultProps(Playground, UICard) export const ButtonAsSlot: Story = { ...Playground, story: { name: 'Button as slots', }, render: (args, { argTypes }) =&gt; ({ components: { UICard, UIButton }, props: Object.keys(argTypes), setup() { return { ...args, } }, template: ` &lt;UICard v-bind="args"&gt; &lt;template v-if="$props.default" v-slot&gt; &lt;p&gt;↓Content of slot displayed below&lt;/p&gt; ${args.default} &lt;/template&gt; &lt;/UICard&gt; `, }), args: { ...Playground.args, default: '&lt;UIButton v-bind="{type: 'button', isDisabled: false, variant: 'primary', label: 'test',}" /&gt;', }, }</pre> <p><br /></p>
P粉504920992
P粉504920992

全部回复(1)
P粉154798196

最后我像这样编写了代码并且它起作用了。

import { Disabled } from 'disabled.stories'

export const ButtonAsSlot: Story = {
  ...Playground,
  args: {
    ...Playground.args,
    disabledArgs: {
      ...Disabled.args,
    },
    default: '<UIButton v-bind="disabledArgs" />',
  },
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号