我正在尝试从main.js文件访问vue.js应用程序中的商店。但出于某种原因,商店 undefined 当我尝试使用它的时候。这是我的代码:
undefined
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') } })
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 } })
但是当我试图从商店里读的时候,它说它是未定义的。我不知道这是为什么?我要进口商店。有人有想法吗?
我觉得上课不方便。
在main.js中: 从“./store/store”导入存储
在您的store.js中尝试此操作: `
export default new Vuex.Store({ plugins: [createPersistedState({ paths: ['auth'] })], state: { }, getters: { }, mutations: { }, modules: { auth, flash, userDropdown, optionDropdown } })
`