从main.js访问商店

时间:2018-07-15 13:40:05

标签: javascript vue.js

我正在尝试从main.js文件访问vue.js应用程序中的商店。但是由于某种原因,当我尝试使用存储时,存储为undefined。这是我的代码:

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

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
  }
})

但是当我尝试从商店中读取信息时,它说是未定义的。我不确定为什么会这样吗?我正在进口商店。有人有想法吗?

1 个答案:

答案 0 :(得分:0)

我觉得在课堂上讲const是个问题。

在main.js中: 从“ ./store/store”导入商店

在store.js中尝试以下操作: `

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

`

相关问题