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

EaB*_*EaB 5 vue.js vuex vuejs3

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

import storeA from '/path';

import storeB from '/path';
Run Code Online (Sandbox Code Playgroud)

现在合并了两家商店

const combinedStore = //combine logic as i don't know this logic
Run Code Online (Sandbox Code Playgroud)

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

这是我的vuex商店在两个地方的样子stores

商店 1:

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

商店2:

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

这是我尝试的方法:

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

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

const newStore = new Vuex.Store({
    ...newStoreData
});
Run Code Online (Sandbox Code Playgroud)

现在只有 1 家商店可以工作,其他商店会抛出类似的错误

读取未定义的名字 (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

Mis*_*ses 6

浏览器 3+

\n

较低版本可能也有这个方法,但我\xe2\x80\x99m 不确定。

\n
import StoreA from \'storeA.js\';\nimport StoreB from \'storeB.js\';\n\nconst newStore = new Vuex.Store(storeA);\n\nnewStore.registerModule(\'\', storeB)\n
Run Code Online (Sandbox Code Playgroud)\n

我刚刚修改了store2.js文件main.js

\n

链接到已解决的问题:https://codesandbox.io/s/vuex-playground-forked-6pg4w ?file=/src/main.js

\n

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

\n