storeB.js
const storeB = {namespaced: true,state: {dataInB: 'This is data in storeB'},mutations: {// 模块B的方法},actions: {// 模块B的动作},getters: {// 模块B的获取器}};export default storeB;
在storeA.js中,要访问storeB的state数据,可以通过rootState在actions或者getters中访问:
const storeA = {namespaced: true,state: {// 模块A的状态},mutations: {// 模块A的方法},actions: {someActionA({ rootState }) {const dataFromB = rootState.storeB.dataInB;console.log(dataFromB);}},getters: {someGetterA({ rootState }) {return rootState.storeB.dataInB;}}};export default storeA;