Replies: 4 comments 2 replies
|
I'm also looking for a way to provide value to a single component. But you could still provide a global value to all components using the setup function in import { setup } from "@storybook/vue3";
import "../src/style.css";
setup((app) => {
app.provide("key", "provided value");
});The other way I could think of is to create a custom wrapper that provide value to the child components as in this. |
|
Using storybook v8 (I'm guessing this is available in v7 as well, but I didn't check), I was able to provide data via |
|
I created a decorator for this which allows you to create specific stories with different values injected/provided via a story's Add this to your preview.js file: const preview = {
decorators: [
(story, { args, parameters }) => {
const valueMap = new Map();
const newArgs = { ...args };
parameters.inject?.forEach((attribute) => {
valueMap.set(attribute, args[attribute]);
delete newArgs[attribute];
});
const WrapperComponent = defineComponent({
setup() {
valueMap.forEach((value, key) => {
provide(
key,
computed(() => value),
);
});
},
template: "<slot />",
});
story({ args: newArgs });
return {
components: { story, WrapperComponent },
template: "<WrapperComponent><story /></WrapperComponent>",
};
},
],
};
export default preview;In your stories you can add: const meta = {
title: "Editor/Home/Home",
component: Home,
args: {
yourInjectedRef: 'and it's value', // provide what you'd like your injected value to be
},
parameters: {
inject: ["yourInjectedRef"], // define what needs to be injected
},I'm currently trying to make all of it type safe but thats pretty tricky. |
|
Altough the So in case storybook with pinia and inject doesn't work, you have to move the setup((app, {parameters}) => {
const pinia = createPinia();
app.use(pinia);
app.use(autoAnimatePlugin);
app.provide('config', parameters?.config); // <<<< this one
});Then you'll use it as usual: |
Uh oh!
There was an error while loading. Please reload this page.
Hiya,
How do I provide data for a component trying to inject the data?
https://vuejs.org/guide/components/provide-inject.html
Thanks,
Nicolaj
All reactions