Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing vuex store in js file

Just like in main.js, I'm trying to access my store from a helper function file:

import store from '../store'  let auth = store.getters.config.urls.auth 

But it logs an error:

Uncaught TypeError: Cannot read property 'getters' of undefined.

I have tried

this.$store.getters.config.urls.auth 

Same result.

store:

//Vuex import Vue from 'vue' import Vuex from 'vuex'  Vue.use(Vuex);  const store = new Vuex.Store({     state: {         config: 'config',      },     getters: {         config: state => state.config     }, });  export default store 

How do I make my store available outside of components?

like image 217
Elena Maximova Avatar asked Dec 14 '17 17:12

Elena Maximova


People also ask

How do I get the Vuex store in JavaScript?

To get Vuex state from a JavaScript file with Vue. js, we can import the Vuex store and use it directly. to import the store from a module. Then we get a state value from the store with store.

Where is Vuex data stored?

In Vuex, our data store is defined as state in the store/index. js config object. Even though it sounds like new terminology to learn, think of it as the data property that we have been using this whole time.


1 Answers

The following worked for me:

import store from '../store'  store.getters.config // => 'config' 
like image 73
Paweł Gościcki Avatar answered Sep 21 '22 10:09

Paweł Gościcki