简体   繁体   English

如何将道具传递给组件的故事?

[英]How to pass props to component's story?

I wanted to add some stories for vue-select component using Storybook , but I'm struggling with more complicated cases, which involve passing props or methods. 我想使用Storybookvue-select组件添加一些故事,但我正在努力处理更复杂的情况,包括传递道具或方法。

When I pass props inside the template it works : 当我在模板中传递道具时,它可以工作

storiesOf('VSelect', module)
.add('with labeled custom options', () => ({
        components: {VSelect},
        template:   `<v-select :options='[{value: "CA", label: "Canada"}, {value: "UK", label: "United Kingdom"}]' />`
    }))

I find it not very readable, so I wanted to pass them separately as props or data: 我发现它不太可读,所以我想将它们作为道具或数据单独传递:

.add('with labeled custom options as props', () => ({
        components: {VSelect},
        props:      {options: [{value: "CA", label: "Canada"}, {value: "UK", label: "United Kingdom"}]},
        data:       {options: [{value: "CA", label: "Canada"}, {value: "UK", label: "United Kingdom"}]},
        template:   `<v-select />`
    }))

but neither data , nor props are not recognized by storybook - they seem to be ignored. 但是dataprops都没有被故事书识别 - 它们似乎被忽略了。

I've solved it. 我已经解决了。

.add('with labeled custom options as props', () => ({
        components: {VSelect},
        data() {
            return {
                options: [{value: "CA", label: "Canada"}, {value: "UK", label: "United Kingdom"}]
            }
        },
        template:   `<v-select :options="options" />`
    }))

There were 2 problems with my former approach: 我以前的方法有两个问题:

  • Passed data was not wrapped in a function 传递的data未包含在函数中
  • I should have pass data only. 我应该只传递data Using both props and data seems to make Vue return a warning (The data property "options" is already declared as a prop.) and ignore passed data (even though it's just a warning not an error, which I find odd) 使用propsdata似乎使Vue返回一个警告(数据属性“options”已经被声明为prop。)并忽略传递的data (即使它只是一个警告而不是错误,我觉得很奇怪)

Have you tried to bind the options in data with the options on your VSelect component? 您是否尝试使用VSelect组件上的选项绑定数据中的选项?

Like this: 像这样:

.add('with labeled custom options as props', () => ({
        components: {VSelect},
        data:       {options: [{value: "CA", label: "Canada"}, {value: "UK", label: "United Kingdom"}]},
        template:   `<v-select :options="options"/>`
    }))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM