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.

How to import Vuex or eventBus.js file in index.js file?

See original GitHub issue

I have to develop the communication between my Vue components, either with Vuex state management or with eventBus, but I cannot import the proper *.js file in my index.js. Vuex has already been installed with npm.

I tried something like this: //********************************************************************** store.js:

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
    state: {
        status: []
    }
});

//********************************************************************** index.js:

import Vuex from 'vuex';
import store from './store/store'


SBA.use({
  install({vue}) {
    vue.use(Vuex)
  }
})

SBA.use({
  install({Vuex}) {
    Vuex.use(store)
  }
})

//********************************************************************** I get the following error:

store.js: Uncaught TypeError: Cannot read property 'use' of undefined
    at eval (store.js:8)
    at Module../src/store/store.js (custom.js:1811)

OR after some modification: Uncaught Error: [vuex] must call Vue.use(Vuex) before creating a store instance

I also tried to this, but nothing worked nothing worked:

import Vuex from 'vuex';
import store from './store/store'

SBA.use({
  install({viewRegistry,vue}) {
    vue.use(Vuex);
    vue.use(store);
    viewRegistry.addView({
      name:   'instances/custom',
      parent: 'instances',
      path:   'custom',
      component: customEndpoint,
      label: 'something',
      group: 'Group',
      order: -10,                   // Orig: 1000
      isEnabled: ({instance}) => instance.hasEndpoint('custom')
    });
  }
});

How shall I import *.js files properly? Thank you for the help!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
joshistecommented, Sep 21, 2020

you need to instantiate the store in the install method. You could turn the store into a promise and resolve it in the install method:

(UNTESTED)

export const store = new Promise(resolve => {
    SBA.use({
        install({viewRegistry, vue}) {
            vue.use(Vuex);

            const store = new Vuex.Store({
                state: {
                    status: [{streamName: '', streamState: []}]
                }
            });

            vue.use(store);
            viewRegistry.addView({
                name: 'instances/custom',
                parent: 'instances',
                path: 'custom',
                component: customEndpoint,
                label: 'GSLA-Cloud Streams',
                group: 'Group',
                order: -10,                   // Orig: 1000
                isEnabled: ({instance}) => instance.hasEndpoint('custom')
            });

            resolve(store)
        }
    });
});
1reaction
joshistecommented, Sep 18, 2020

the Vue.use in the store.js is the problem you should remove this and you also need to move the creation of the store into the install callback.

Read more comments on GitHub >

github_iconTop Results From Across the Web

accessing vuex store in js file - Stack Overflow
The following worked for me: import store from '../store' store.getters.config // => 'config'.
Read more >
Using event bus in Vue.js to pass data between components
We're going to create an event bus instance as a separate file, import it into the two components that are going to share...
Read more >
Adding the modules to Vuex | Vue.js 3 Cookbook
Open the index.js file in the src/store folder. Import the index.js file from the authentication folder: import Vue from 'vue'; import Vuex from...
Read more >
Event Bus implementation in Vue - Binod Mahto - Medium
Step 1: First we will be creating a vuejs application and then creating a global instance of the event bus using the mitt...
Read more >
[Solved]-Vue.js 3 Event Bus-Vue.js - appsloveworld.com
Create a file src/composables/useEmitter.js import { getCurrentInstance } from 'vue' export default function useEmitter() { const internalInstance ...
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