Storybook装饰器里的参数为什么没生效?
我在用Vue Storybook给按钮组件加一个主题装饰器,想通过参数切换主题样式,但发现传递的参数完全没反应…
写了个装饰器函数:
export const withTheme = (storyFn, { theme }) => {
return (
{storyFn()}
);
};
然后在按钮的故事里这样写:
export default {
title: 'Atoms/Button',
decorators: [
(story) => withTheme(story, { theme: 'lightblue' })
]
};
结果背景色根本没变,控制台也没报错,明明参数传递方式参考了官方文档啊,哪里出问题了?
export const withTheme = (storyFn, context) => {
const theme = context.parameters.theme;
return (
{storyFn()}
);
};
另外确保在预览配置里加了参数支持,
.storybook/preview.js加上这行:export const parameters = {
theme: 'lightblue'
};
搞定。
withTheme装饰器没正确嵌套故事内容,同时也没应用主题样式。直接改成这样:记得把参数加到
parameters里,装饰器自动读取。