question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue $root component data access or setup

See original GitHub issue

If 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
graupcommented, Jun 21, 2019

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:

addDecorator({
  i18n,
  beforeCreate: function() {
    this.$root._i18n = this.$i18n;
  },
  template: '<div><slot /></div>',
})
1reaction
linasmatakascommented, Oct 31, 2019

@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:

  1. Example of how to create global variables. Note that this service does more than it needs for isMobile to work, but I think that is self-sufficient, so it should be easy to try it out. https://gist.github.com/linasmatakas/94a515f78228f023f250a1f959e073b0
  2. main.js: 2.1. import WindowService from '@/services/window-service'; 2.2. beforeMount() { WindowService.init(); },
  3. Usage in template $screen.isMobile
  4. .storybook/config.js: import and WindowService.init(); anywhere
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found