代码之家  ›  专栏  ›  技术社区  ›  Bitwise

从main.js访问存储

  •  0
  • Bitwise  · 技术社区  · 7 年前

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

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

    但是当我试图从商店里读的时候,它说它是未定义的。我不知道这是为什么?我要进口商店。有人有想法吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Deepesh    7 年前

    我觉得上课不方便。

    在main.js中: 从“./store/store”导入存储

    在您的store.js中尝试此操作: `

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

    `

    推荐文章