javascript 访问 js 文件中的 vuex 存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47819289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing vuex store in js file
提问by Elena Maximova
Just like in main.js, I'm trying to access my store from a helper function file:
就像在 main.js 中一样,我试图从辅助函数文件访问我的商店:
import store from '../store'
let auth = store.getters.config.urls.auth
But it logs an error:
但它记录了一个错误:
Uncaught TypeError: Cannot read property 'getters' of undefined.
未捕获的类型错误:无法读取未定义的属性“getter”。
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?
如何使我的商店在组件之外可用?
回答by Pawe? Go?cicki
The following worked for me:
以下对我有用:
import store from '../store'
store.getters.config
// => 'config'
回答by EJL
put brackets on your import and it should work
将括号放在您的导入上,它应该可以工作
import { store } from '../store'

