accessing vuex store in js file

感情迁移 提交于 2020-07-18 08:50:07

问题


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?


回答1:


The following worked for me:

import store from '../store'

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



回答2:


put brackets on your import and it should work

import { store } from '../store'


来源:https://stackoverflow.com/questions/47819289/accessing-vuex-store-in-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!