accessing vuex store in js file

Issue

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?

Solution

The following worked for me:

import store from '../store'

store.getters.config
// => 'config'

Answered By – Paweł Gościcki

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published