首页 > 解决方案 > 从 main.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')
  }
})

商店/商店.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
  }
})

但是当我尝试从商店读取时,它说它是未定义的。我不确定这是为什么?我正在导入商店。有人有想法吗?

标签: javascriptvue.js

解决方案


我觉得课堂上的 const 是问题所在。

main.js

import store from './store/store'

在你的 store.js 中试试这个:`

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

`


推荐阅读