Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

vuex 子module 的mutation 如何修改根module 的state?

子modules 无state数据,打算只操作根的state。

export default new Vuex.Store({
    state: {
        // 希望 aa能操作这里的数据。
    },
    getters: {
    },
    mutations: {   
    },
    actions: {
    },
    
    modules: {
        AAA: {
            namespaced: true,
            mutations: {
                aa(state,data){
                    //这里如何修改根state的数据 。rootState
                }
                
            }

        }
    }
})


store.commit(`AAA/aa`,data)

我的需求是这样的:

我想多页面之间通信,同步vuex的state,通过监听storage事件来实现,A页面用 mutations修改了state,并且里面触发storage事件。B页面监听到后。为了避免循环触发storage事件,要在另一份mutations里修改state。

两份mutations其实代码一样,只是为了操作同一份state。

请问有什么其他方法吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

action 里面是可以拿到 rootState的

你说的这个需求应该别人有实现过了吧 搜了下这个, 看描述应该跟你差不多


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...