如何将 2 个 vuex 商店合并为一个商店

How to combine 2 vuex stores into a single store

您好,我有一个将 2 vuex 家商店合并为一个商店的要求(两家商店属于不同的项目

import storeA from '/path';

import storeB from '/path';

现在合并两个商店

const combinedStore = //combine logic as i don't know this logic

我在 Whosebug 上搜索了很多以合并 2 个 vuex 存储,但我没有找到任何发布的解决方案。

这是我的 vuex 商店在 stores

中的样子

商店 1:

   // storeA.js
    
    const store = {
        state(){return {}},
        actions: {async getData(){...}},
        mutations: {},
        getters: {}, 
        modules:{ 
           Login
        }
    }

店铺2:

   // storeB.js
    
    const store = {
        state(){return {}},
        actions: {async getUsers(){...}},
        mutations: {},
        getters: {}, 
        modules:{ 
           workflow
        }
    }

我是这样尝试的:

import StoreA from 'storeA.js';
import StoreB from 'storeB.js';

const newStoreData = Object.assign({},StoreA,StoreB)

const newStore = new Vuex.Store({
    ...newStoreData
});

现在只有 1 个商店可以正常工作,其他商店会抛出类似

的错误

reading first_name name of undefined (i,e state.[module].first_name)

问题可以在这里重现: https://codesandbox.io/s/vuex-playground-forked-1h344?file=/src/main.js

原始工作叉: https://codesandbox.io/s/k012qvkmnv?file=/src/main.js

Vue 3+

低版本应该也有这个方法,但我不确定。

import StoreA from 'storeA.js';
import StoreB from 'storeB.js';

const newStore = new Vuex.Store(storeA);

newStore.registerModule('', storeB)

我刚刚修改了 store2.jsmain.js 个文件

link 解决问题:https://codesandbox.io/s/vuex-playground-forked-6pg4w?file=/src/main.js

这是关于它的文档:https://vuex.vuejs.org/guide/modules.html#dynamic-module-registration