Vue $root component data access or setup
See original GitHub issueIf Vue project’s $root component has a reactive variable (in data or computed) f.e. isMobile
, so it can be used in app’s components as $root.isMobile
, then there is no way to define it in config.js
or anywhere globally for storybook usage.
It would be nice to have some option to define $root component’s data, computed and methods.
Alternative solution that I use now, but it has annoying performance and usability cases:
Add variable as Vue.prototype.isMobile
const localEventBus = new Vue();
Vue.mixin({
mounted() {
localEventBus.$on('storybook-rerender', this.storybookForceUpdate);
},
beforeDestroy() {
localEventBus.$off('storybook-rerender', this.storybookForceUpdate);
},
methods: {
storybookForceUpdate() {
this.$forceUpdate();
},
},
});
window.addEventListener('resize', () => {
localEventBus.$emit('storybook-rerender');
});
Does anyone have alternative solutions? or perhaps there is a way to add data or computed to root component in storybook?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:18 (6 by maintainers)
Top Results From Across the Web
VueJS accessing data from component in root Vue instance
I'm trying to access a component's data in my root Vue instance. What I'm trying to accomplish is, you click on a button...
Read more >Handling Edge Cases - Vue.js
Accessing the Parent Component Instance. Similar to $root , the $parent property can be used to access the parent instance from a child....
Read more >Creating a Vue Application - Vue.js
The object we are passing into createApp is in fact a component. Every app requires a "root component" that can contain other components...
Read more >Components Basics - Vue.js
Since components are reusable Vue instances, they accept the same options as new Vue , such as data , computed , watch ,...
Read more >Components Basics - Vue.js
This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s a sort of ugly workaround for this. Just add a decorator that wraps every story and manually assign $root in there. Something along the lines of this:
@naimlatifi5 I cannot offer a fix for the original issue, but if you are interested in a bit more elegant solution of how to use global variables, which also easily works in storybook as well then here is a quick try at that:
isMobile
to work, but I think that is self-sufficient, so it should be easy to try it out. https://gist.github.com/linasmatakas/94a515f78228f023f250a1f959e073b0main.js
: 2.1.import WindowService from '@/services/window-service';
2.2.beforeMount() { WindowService.init(); },
$screen.isMobile
.storybook/config.js
: import andWindowService.init();
anywhere