带导入的ES6 vuex导入文件(嵌套导入)

时间:2017-10-09 13:43:52

标签: javascript webpack vue.js vuejs2 vuex

我正在使用VUEX在VUE中构建一个webapp 我的代码有一个基础“层”,我需要在每个项目中使用默认代码,所以我不必一次又一次地构建它。

我希望有一个我可以导入的文件,当它是导入时我想要加载每个模块,基础层的默认模块,以及特定于app / project的模块

我的目录结构:

  • SRC
    • 供应商
      • 存储
        • 模块
          • 默认模块1
          • 默认模块2
        • store.js(加载默认模块)
    • 存储
      • 模块
        • 模块1
        • 模块2
      • store.js(veux商店对象)
      • storeloader.js(加载store.js和所有默认模块,供应商store.js加载供应商默认模块)

供应商/商店/ store.js

import { apiRoutes } from './modules/apiRoutes';
import { authorization } from './modules/authorization';
import { store } from '@/stores/storeLoader';
store.registerModule('apiRoutes', apiRoutes);
store.registerModule('authorization', authorization);

stores / store.js(app / project store,vuex商店对象)

import Vue from 'vue';
import Vuex from 'vuex';
import LocalStorage from '@/vendor/kingscode/base/LocalStorage';
Vue.use(Vuex);

export const store = new Vuex.Store({
    strict: true,
    state: LocalStorage.loadState('vuex_app') || {},

    /**
     * Getters.
     * (i.e. computed properties for stores)
     *
     * @see https://vuex.vuejs.org/en/getters.html
     */
    getters: {
        /**
         * Get the selected location id.
         *
         * @param state
         * @param getters
         */
        locationSelectedId: (state, getters) => () => {
            return state.location.id;
        },

        /**
         * Get the selected location id.
         *
         * @param state
         * @param getters
         */
        locationSelectedName: (state, getters) => () => {
            return state.location.name;
        }
    },

    /**
     * Mutations.
     *
     * @see https://vuex.vuejs.org/en/mutations.html
     */
    mutations: {

        /**
         * Set the location object.
         *
         * @param state
         * @param location
         */
        changeLocation(state, location) {
            let newUser = state.user;
            state.user.location = location
            Vue.set(state, 'user', newUser);
            LocalStorage.saveState('vuex_app', state);
        }
    },

    /**
     * Actions.
     *
     * @see https://vuex.vuejs.org/en/actions.html
     */
    actions: {

        //
    },
});

stores / storeloader.js

import { store } from './store.js';
import { cart } from './modules/cart';
store.registerModule('cart', cart);
require('@/vendor/kingscode/stores/store');

此代码的问题在于某些点store无法识别,有时并非每个模块都加载了它。我正在寻找正确的方法,或者完全不同的方式/想法。也许我正在做的事情不允许或不可能这样。

我希望有人理解我的意思,可以帮助我,我已经搜索了很多但也不知道我正在寻找什么,任何帮助表示赞赏

0 个答案:

没有答案
相关问题