简体   繁体   English

从 main.js 访问存储

[英]Access store from main.js

I'm trying to access my store in my vue.js app from my main.js file.我正在尝试从我的 main.js 文件访问我的 vue.js 应用程序中的商店。 But for some reason store is undefined when I try to use it.但是由于某种原因,当我尝试使用它时,商店是undefined Here is my code:这是我的代码:

main.js主文件

import { store } from './store/store'

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !store.getters.isLoggedIn) {
    next({ path: '/' })
  } else if (to.path === '/' && store.getters.isLoggedIn) {
    next({path: '/dashboard'})
  } else if (store.getters.isLoggedIn && !to.meta.requiresAuth) {
    next({path: '/dashboard'})
  } else {
    next()
    store.commit('CLOSE_USER_DROPDOWN')
  }
})

store/store.js商店/store.js

import Vue from 'vue'
import Vuex from 'vuex'
import auth from './modules/auth'
import optionDropdown from './modules/option_dropdown'
import userDropdown from './modules/user_dropdown'
import flash from './modules/flash'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export const store = new Vuex.Store({
  plugins: [createPersistedState({ paths: ['auth'] })],
  state: {
  },
  getters: {
  },
  mutations: {
  },
  modules: {
    auth,
    flash,
    userDropdown,
    optionDropdown
  }
})

But when I try to read from the store it says it's undefined.但是当我尝试从商店读取时,它说它是未定义的。 I'm not sure why this is?我不确定这是为什么? I'm importing the store.我正在导入商店。 Does anybody have ideas?有人有想法吗?

I feel const on class is the problem.我觉得课堂上的常量是问题所在。

In main.js :main.js

import store from './store/store'

Try this in your store.js: `在你的 store.js 中试试这个:`

export default new Vuex.Store({
plugins: [createPersistedState({ paths: ['auth'] })],
state: {
},
getters: {
},
mutations: {
},
modules: {
auth,
flash,
userDropdown,
optionDropdown
}
})

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM